Skip to content

Instantly share code, notes, and snippets.

@alexanderjeurissen
Created August 13, 2023 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexanderjeurissen/e17566335d2fe6b378d6636d625fd35d to your computer and use it in GitHub Desktop.
Save alexanderjeurissen/e17566335d2fe6b378d6636d625fd35d to your computer and use it in GitHub Desktop.
json deserialisation in Go
package main
import (
"encoding/json"
"fmt"
)
type Person struct {
Name string `json:"naam"`
Age int `json:"leeftijd"`
}
func (person *Person) Greet() {
fmt.Printf("Hello %s, I see you are %d years old", person.Name, person.Age)
}
func main() {
jsonString := `
[
{
"naam": "carlos",
"leeftijd": 28
},
{
"naam": "alexander",
"leeftijd": 32
}
]
`
people := []Person{}
err := json.Unmarshal([]byte(jsonString), &people)
if err != nil {
fmt.Println("some error happened while decoding the json")
}
fmt.Println(people)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment