Skip to content

Instantly share code, notes, and snippets.

@timoyuen
Created May 11, 2018 08:16
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 timoyuen/f1d28caf24ef8aab7af4473d4987d5f2 to your computer and use it in GitHub Desktop.
Save timoyuen/f1d28caf24ef8aab7af4473d4987d5f2 to your computer and use it in GitHub Desktop.
An example of a JSON Unmarshal into a Go struct.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type Artist struct {
Id int
Name string
Resource_url string
Releases_url string
Uri string
Realname string
Profile string
Data_quality string
Namevariations []string
Aliases []struct {
Id int
Name string
Resource_url string
}
Urls []string
Images []struct {
Type string
Width int
Height int
Uri string
Uri150 string
Resource_url string
}
}
func main() {
//Query an artist.
res, _ := http.Get("http://api.discogs.com/artists/1373")
temp, _ := ioutil.ReadAll(res.Body)
var artist Artist
err := json.Unmarshal(temp, &artist)
if err != nil {
fmt.Println("There was an error:", err)
}
fmt.Println(artist.Name)
// fmt.Println(artist.Profile)
// fmt.Println(artist.Releases_url)
// fmt.Println(artist.Namevariations)
fmt.Println(artist.Images[1].Height)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment