How to access the different struct variable using other struct variable?










-4















I have two structs and one containing one field and other contains three fields:-



type User struct 
Name CustomerDetails `json:"name" bson:"name"`

type CustomerDetails struct
Value string `json:"value" bson:"value"`
Note string `json:"note" bson:"note"`
SendNotifications bool `json:"send_notifications" bson:"send_notifications"`



I want to access the CustomerDetails fields using the User struct field like



func main() 
var custName User
custName.Name.Value = "ABC"
fmt.Println(custName)



But It gives me the error of




custName.Name.Value undefined (type CustomerDetails has no field or method Value)




Playground link



How will I solve this error? Can anyone help me?










share|improve this question

















  • 2





    Because Name is a slice. You should use respective indexes to set the value on them. If there will be only one CustomerDetail for each user, then replace CustomerDetails to CustomerDetails.

    – Berkant
    Nov 16 '18 at 10:02












  • @Berkant I Can't change it like you say. Is there is another method without changing the struct?

    – msd
    Nov 16 '18 at 10:18












  • You may try appending to custName.Name. play.golang.org/p/PB9y5svCWwT

    – Berkant
    Nov 16 '18 at 10:20















-4















I have two structs and one containing one field and other contains three fields:-



type User struct 
Name CustomerDetails `json:"name" bson:"name"`

type CustomerDetails struct
Value string `json:"value" bson:"value"`
Note string `json:"note" bson:"note"`
SendNotifications bool `json:"send_notifications" bson:"send_notifications"`



I want to access the CustomerDetails fields using the User struct field like



func main() 
var custName User
custName.Name.Value = "ABC"
fmt.Println(custName)



But It gives me the error of




custName.Name.Value undefined (type CustomerDetails has no field or method Value)




Playground link



How will I solve this error? Can anyone help me?










share|improve this question

















  • 2





    Because Name is a slice. You should use respective indexes to set the value on them. If there will be only one CustomerDetail for each user, then replace CustomerDetails to CustomerDetails.

    – Berkant
    Nov 16 '18 at 10:02












  • @Berkant I Can't change it like you say. Is there is another method without changing the struct?

    – msd
    Nov 16 '18 at 10:18












  • You may try appending to custName.Name. play.golang.org/p/PB9y5svCWwT

    – Berkant
    Nov 16 '18 at 10:20













-4












-4








-4








I have two structs and one containing one field and other contains three fields:-



type User struct 
Name CustomerDetails `json:"name" bson:"name"`

type CustomerDetails struct
Value string `json:"value" bson:"value"`
Note string `json:"note" bson:"note"`
SendNotifications bool `json:"send_notifications" bson:"send_notifications"`



I want to access the CustomerDetails fields using the User struct field like



func main() 
var custName User
custName.Name.Value = "ABC"
fmt.Println(custName)



But It gives me the error of




custName.Name.Value undefined (type CustomerDetails has no field or method Value)




Playground link



How will I solve this error? Can anyone help me?










share|improve this question














I have two structs and one containing one field and other contains three fields:-



type User struct 
Name CustomerDetails `json:"name" bson:"name"`

type CustomerDetails struct
Value string `json:"value" bson:"value"`
Note string `json:"note" bson:"note"`
SendNotifications bool `json:"send_notifications" bson:"send_notifications"`



I want to access the CustomerDetails fields using the User struct field like



func main() 
var custName User
custName.Name.Value = "ABC"
fmt.Println(custName)



But It gives me the error of




custName.Name.Value undefined (type CustomerDetails has no field or method Value)




Playground link



How will I solve this error? Can anyone help me?







go






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 9:55









msdmsd

43




43







  • 2





    Because Name is a slice. You should use respective indexes to set the value on them. If there will be only one CustomerDetail for each user, then replace CustomerDetails to CustomerDetails.

    – Berkant
    Nov 16 '18 at 10:02












  • @Berkant I Can't change it like you say. Is there is another method without changing the struct?

    – msd
    Nov 16 '18 at 10:18












  • You may try appending to custName.Name. play.golang.org/p/PB9y5svCWwT

    – Berkant
    Nov 16 '18 at 10:20












  • 2





    Because Name is a slice. You should use respective indexes to set the value on them. If there will be only one CustomerDetail for each user, then replace CustomerDetails to CustomerDetails.

    – Berkant
    Nov 16 '18 at 10:02












  • @Berkant I Can't change it like you say. Is there is another method without changing the struct?

    – msd
    Nov 16 '18 at 10:18












  • You may try appending to custName.Name. play.golang.org/p/PB9y5svCWwT

    – Berkant
    Nov 16 '18 at 10:20







2




2





Because Name is a slice. You should use respective indexes to set the value on them. If there will be only one CustomerDetail for each user, then replace CustomerDetails to CustomerDetails.

– Berkant
Nov 16 '18 at 10:02






Because Name is a slice. You should use respective indexes to set the value on them. If there will be only one CustomerDetail for each user, then replace CustomerDetails to CustomerDetails.

– Berkant
Nov 16 '18 at 10:02














@Berkant I Can't change it like you say. Is there is another method without changing the struct?

– msd
Nov 16 '18 at 10:18






@Berkant I Can't change it like you say. Is there is another method without changing the struct?

– msd
Nov 16 '18 at 10:18














You may try appending to custName.Name. play.golang.org/p/PB9y5svCWwT

– Berkant
Nov 16 '18 at 10:20





You may try appending to custName.Name. play.golang.org/p/PB9y5svCWwT

– Berkant
Nov 16 '18 at 10:20












2 Answers
2






active

oldest

votes


















0














type User struct 
Name CustomerDetails `json:"name" bson:"name"`



Here, User.Name is slice, that's why you are getting error.



func main() 
var custName User
custName.Name = append(custName.Name, CustomerDetails
Value: "ABC",
)
fmt.Println(custName)



https://play.golang.org/p/J56LjH7Lqdd






share|improve this answer






























    0














    You should append the CustomerDetails to User.Name like so: https://play.golang.org/p/jk73roZiAC2



    var custName User

    cd := CustomerDetails
    Value: "ABC",
    Note: "Test",


    custName.Name = append(custName.Name, cd)

    fmt.Println(custName)


    User.Name is a slice so you can't give it a single value.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335351%2fhow-to-access-the-different-struct-variable-using-other-struct-variable%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      type User struct 
      Name CustomerDetails `json:"name" bson:"name"`



      Here, User.Name is slice, that's why you are getting error.



      func main() 
      var custName User
      custName.Name = append(custName.Name, CustomerDetails
      Value: "ABC",
      )
      fmt.Println(custName)



      https://play.golang.org/p/J56LjH7Lqdd






      share|improve this answer



























        0














        type User struct 
        Name CustomerDetails `json:"name" bson:"name"`



        Here, User.Name is slice, that's why you are getting error.



        func main() 
        var custName User
        custName.Name = append(custName.Name, CustomerDetails
        Value: "ABC",
        )
        fmt.Println(custName)



        https://play.golang.org/p/J56LjH7Lqdd






        share|improve this answer

























          0












          0








          0







          type User struct 
          Name CustomerDetails `json:"name" bson:"name"`



          Here, User.Name is slice, that's why you are getting error.



          func main() 
          var custName User
          custName.Name = append(custName.Name, CustomerDetails
          Value: "ABC",
          )
          fmt.Println(custName)



          https://play.golang.org/p/J56LjH7Lqdd






          share|improve this answer













          type User struct 
          Name CustomerDetails `json:"name" bson:"name"`



          Here, User.Name is slice, that's why you are getting error.



          func main() 
          var custName User
          custName.Name = append(custName.Name, CustomerDetails
          Value: "ABC",
          )
          fmt.Println(custName)



          https://play.golang.org/p/J56LjH7Lqdd







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 10:29









          nightfury1204nightfury1204

          1,779510




          1,779510























              0














              You should append the CustomerDetails to User.Name like so: https://play.golang.org/p/jk73roZiAC2



              var custName User

              cd := CustomerDetails
              Value: "ABC",
              Note: "Test",


              custName.Name = append(custName.Name, cd)

              fmt.Println(custName)


              User.Name is a slice so you can't give it a single value.






              share|improve this answer



























                0














                You should append the CustomerDetails to User.Name like so: https://play.golang.org/p/jk73roZiAC2



                var custName User

                cd := CustomerDetails
                Value: "ABC",
                Note: "Test",


                custName.Name = append(custName.Name, cd)

                fmt.Println(custName)


                User.Name is a slice so you can't give it a single value.






                share|improve this answer

























                  0












                  0








                  0







                  You should append the CustomerDetails to User.Name like so: https://play.golang.org/p/jk73roZiAC2



                  var custName User

                  cd := CustomerDetails
                  Value: "ABC",
                  Note: "Test",


                  custName.Name = append(custName.Name, cd)

                  fmt.Println(custName)


                  User.Name is a slice so you can't give it a single value.






                  share|improve this answer













                  You should append the CustomerDetails to User.Name like so: https://play.golang.org/p/jk73roZiAC2



                  var custName User

                  cd := CustomerDetails
                  Value: "ABC",
                  Note: "Test",


                  custName.Name = append(custName.Name, cd)

                  fmt.Println(custName)


                  User.Name is a slice so you can't give it a single value.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 10:34









                  VizjereiVizjerei

                  629312




                  629312



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335351%2fhow-to-access-the-different-struct-variable-using-other-struct-variable%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      天津地下鉄3号線

                      How to fill missing numeric if any value in a subset is missing, all other columns with the same subset are missing