Created
August 13, 2023 12:24
-
-
Save alexanderjeurissen/e17566335d2fe6b378d6636d625fd35d to your computer and use it in GitHub Desktop.
json deserialisation in Go
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" | |
) | |
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