Quantcast
Channel: How can I merge two structs in Golang? - Stack Overflow
Browsing latest articles
Browse All 6 View Live

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 Article


Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


How 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
Browsing latest articles
Browse All 6 View Live


Latest Images