Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Golang combine two structs with json.Marshal] #go #golang #struct #json #marshal #combine
package main
import (
"encoding/json"
"fmt"
)
type A struct {
Name string `json:"name"`
Description string `json:"description"`
URL string `json:"url"`
}
type B struct {
Name string `json:"name"`
*A
}
func main() {
a := A{Name: "test", Description: "desc", URL: "https://example.com"}
b := B{Name: "new name"}
b.A = &a
data, _ := json.Marshal(b)
fmt.Println(string(data))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment