Last active
August 29, 2015 14:03
-
-
Save superbrothers/466cfef9df99a3dd59c5 to your computer and use it in GitHub Desktop.
Access GitHub API with golang
This file contains 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" | |
"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