Skip to content

Instantly share code, notes, and snippets.

@superbrothers
Last active August 29, 2015 14:03
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 superbrothers/466cfef9df99a3dd59c5 to your computer and use it in GitHub Desktop.
Save superbrothers/466cfef9df99a3dd59c5 to your computer and use it in GitHub Desktop.
Access GitHub API with golang
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
type User struct {
Login string `json:"login"`
HtmlUrl string `json:"html_url"`
}
func main() {
res, err := http.Get("https://api.github.com/users/superbrothers")
if err != nil {
log.Fatal(err)
os.Exit(1)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
user := User{}
err = json.Unmarshal(body, &user)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
fmt.Printf("login: %s\nhtml_url: %s\n", user.Login, user.HtmlUrl)
os.Exit(0)
}
/*
% go run app.go
login: superbrothers
html_url: https://github.com/superbrothers
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment