Answer by Mahen Gandhi for How can I merge two structs in Golang?
One approach to this is to use the inbuilt reflect package and create a new struct programmatically!func stringInSlice(value string, slice []string) bool { for _, elem := range slice { if elem == value...
View ArticleAnswer by Adrien Parrochia for How can I merge two structs in Golang?
You can merge two struct like this :package mainimport ("fmt""encoding/json")type b struct { Name string `json:"name"` Description string Url string}type a struct { *b MimeType string...
View ArticleAnswer by Elias Van Ootegem for How can I merge two structs in Golang?
Go is all about composition over inheritance. Sadly, you're using anonymous structs, but given that you're clearly trying to json marshal them, you're better of defining them as types:type name struct...
View ArticleAnswer by nothingmuch for How can I merge two structs in Golang?
You can embed both structs in another.type name struct { Name string `json:"name"`}type description struct { Description string `json:"description"`}type combined struct { name description}The JSON...
View ArticleAnswer by Franck Jeannin for How can I merge two structs in Golang?
It's a bit convoluted but I suppose you could do something like this: a := struct { Name string `json:"name"` }{"my name"} b := struct { Description string `json:"description"` }{"my description"} var...
View ArticleHow can I merge two structs in Golang?
I have two json-marshallable anonymous structs.a := struct { Name string `json:"name"`}{"my name"}b := struct { Description string `json:"description"`}{"my description"}Is there any way to merge them...
View Article