Last active
December 14, 2015 12:39
-
-
Save ecavazos/5088025 to your computer and use it in GitHub Desktop.
Get json data from Github's API (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" | |
| "io/ioutil" | |
| "net/http" | |
| ) | |
| func gist(id string) { | |
| var i interface{} | |
| get("https://api.github.com/gists/"+id, &i) | |
| m := i.(map[string]interface{}) | |
| for k, v := range m { | |
| fmt.Printf("%v: %v\n\n", k, v) | |
| } | |
| } | |
| func user(name string) { | |
| var i map[string]interface{} | |
| get("https://api.github.com/users/"+name, &i) | |
| for k, v := range i { | |
| fmt.Printf("%v: %v\n\n", k, v) | |
| } | |
| } | |
| func repos(user string) { | |
| url := fmt.Sprintf("https://api.github.com/users/%s/repos", user) | |
| var i []interface{} | |
| get(url, &i) | |
| for k, v := range i { | |
| fmt.Printf("%v: %v\n\n", k, v) | |
| } | |
| } | |
| func get(url string, i interface{}) { | |
| resp, err := http.Get(url) | |
| if err != nil { | |
| panic(err) | |
| } | |
| repos, _ := ioutil.ReadAll(resp.Body) | |
| json.Unmarshal(repos, &i) | |
| } | |
| func main() { | |
| user("ecavazos") | |
| repos("ecavazos") | |
| gist("5080066") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment