Last active
October 26, 2019 12:15
-
-
Save lachlan-eagling/4a177badbf7bb1b22114bdaf031e0e5b to your computer and use it in GitHub Desktop.
Blog - Anatomy of a Struct (Unmarshaling)
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 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