Created
April 25, 2016 19:17
-
-
Save FilBot3/d121011ff1d059160e78914f3f58e93b to your computer and use it in GitHub Desktop.
Nested JSON in GOLang
This file contains hidden or 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" | |
| ) | |
| func chkerr(err error) { | |
| if err != nil { | |
| panic(err) | |
| } | |
| } | |
| type Music struct { | |
| Artist string `json:"Artist"` | |
| // Nest or include another struct as an embedded JSON Element | |
| Album // Embedding the Structure Album | |
| Genre string `json:"Genre"` | |
| } | |
| type Album struct { | |
| Year string `json:"Year"` | |
| Track string `json:"Track"` | |
| } | |
| func main() { | |
| fmt.Printf("Showing some nested JSON\n") | |
| message := Music{ | |
| Artist: "John Anderson", | |
| Album: Album{ | |
| Year: "2016", | |
| Track: "Seminole Wind", | |
| }, | |
| Genre: "Country", | |
| } | |
| fmt.Println(message) | |
| json_message, err := json.Marshal(message) | |
| chkerr(err) | |
| fmt.Println(json_message) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment