How to access the different struct variable using other struct variable?
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
add a comment |
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
2
BecauseName
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 replaceCustomerDetails
toCustomerDetails
.
– 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 tocustName.Name
. play.golang.org/p/PB9y5svCWwT
– Berkant
Nov 16 '18 at 10:20
add a comment |
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
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
go
asked Nov 16 '18 at 9:55
msdmsd
43
43
2
BecauseName
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 replaceCustomerDetails
toCustomerDetails
.
– 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 tocustName.Name
. play.golang.org/p/PB9y5svCWwT
– Berkant
Nov 16 '18 at 10:20
add a comment |
2
BecauseName
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 replaceCustomerDetails
toCustomerDetails
.
– 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 tocustName.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
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Nov 16 '18 at 10:29
nightfury1204nightfury1204
1,779510
1,779510
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 16 '18 at 10:34
VizjereiVizjerei
629312
629312
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 replaceCustomerDetails
toCustomerDetails
.– 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