Skip to content

Instantly share code, notes, and snippets.

@Clivern
Created October 1, 2018 16:27
Show Gist options
  • Save Clivern/742f511e8554b1510c9139a1ecaf1dc4 to your computer and use it in GitHub Desktop.
Save Clivern/742f511e8554b1510c9139a1ecaf1dc4 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