Created
November 27, 2020 00:37
-
-
Save madflojo/645facb5519d91f3f59f76c8c00354cd to your computer and use it in GitHub Desktop.
maps.vs.structs.structparser
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" | |
) | |
// Example is our main data structure used for JSON parsing | |
type Example struct { | |
Name string `json:"name"` | |
Numbers []int `json:"numbers"` | |
Nested Nested `json:"nested"` | |
} | |
// Nested is an embedded structure within Example | |
type Nested struct { | |
IsIt bool `json:"isit"` | |
Description string `json:"description"` | |
} | |
func main() { | |
// Define a JSON string | |
j := `{"name":"example","numbers":[1,2,3,4],"nested":{"isit":true,"description":"a nested json"}}` | |
// Parse JSON string into data object | |
var data Example | |
err := json.Unmarshal([]byte(j), &data) | |
if err != nil { | |
fmt.Printf("Error parsing JSON string - %s", err) | |
} | |
// Print the name | |
fmt.Printf("Name is %s", data.Name) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment