Skip to content

Instantly share code, notes, and snippets.

@mwmahlberg
Created January 22, 2016 22:02
Show Gist options
  • Save mwmahlberg/2a66e7190ce9d69b6064 to your computer and use it in GitHub Desktop.
Save mwmahlberg/2a66e7190ce9d69b6064 to your computer and use it in GitHub Desktop.
Using json.Decoder to decode API call.
package main
import (
"encoding/json"
"fmt"
"net/http"
)
// So ungefähr
type Contrib struct {
Id string `json:"userid"`
Name string `json:"user"`
Title string
}
type Query struct {
Contribs []Contrib `json:"usercontribs"`
}
type Response struct {
Query Query
}
const (
url = "http://www.funtoo.org/api.php?action=query&list=usercontribs&format=json&ucuser=%s&ucdir=newer"
)
func main() {
res, err := http.Get(fmt.Sprintf(url,"jubalh"))
if err != nil {
panic(err)
}
r := Response{}
dec := json.NewDecoder(res.Body)
if err := dec.Decode(&r); err != nil {
panic(err)
}
fmt.Printf("%v\n", len(r.Query.Contribs))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment