Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Blog - Anatomy of a Struct (Unmarshaling)
package main
import (
"encoding/json"
"fmt"
)
type User struct {
FirstName string `json:"firstName"`
LastName string `json:"surname"`
Username string `json:"username"`
Age int `json:"age"`
}
func main() {
var user User
userJson := []byte(`{"firstName": "Lachlan", "surname": "Eagling", "username": "Lachlan_E", "age": 28}`)
if err := json.Unmarshal(userJson, &user); err != nil {
fmt.Println(err)
}
fmt.Printf("FirstName: %s | LastName: %s | Username: %s, | Age: %d", user.FirstName, user.LastName, user.Username, user.Age)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment