[Golang combine two structs with json.Marshal] #go #golang #struct #json #marshal #combine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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