Skip to content

Instantly share code, notes, and snippets.

@eiyaya
Created December 21, 2018 13:40
Show Gist options
  • Save eiyaya/6969168933a7fd0200ccb492eab7e3ab to your computer and use it in GitHub Desktop.
Save eiyaya/6969168933a7fd0200ccb492eab7e3ab to your computer and use it in GitHub Desktop.
Go program that uses json.Unmarshal
package main
import (
"encoding/json"
"fmt"
)
type Language struct {
Id int
Name string
}
func main() {
// String contains two JSON rows.
text := "[{\"Id\": 100, \"Name\": \"Go\"}, {\"Id\": 200, \"Name\": \"Java\"}]"
// Get byte slice from string.
bytes := []byte(text)
// Unmarshal string into structs.
var languages []Language
json.Unmarshal(bytes, &languages)
// Loop over structs and display them.
for l := range languages {
fmt.Printf("Id = %v, Name = %v", languages[l].Id, languages[l].Name)
fmt.Println()
}
}
@eiyaya
Copy link
Author

eiyaya commented Dec 21, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment