Skip to content

Instantly share code, notes, and snippets.

@leejarvis
Created August 8, 2012 14:56
Show Gist options
  • Save leejarvis/3295636 to your computer and use it in GitHub Desktop.
Save leejarvis/3295636 to your computer and use it in GitHub Desktop.
package github
import (
"encoding/json"
"errors"
"fmt"
"net/http"
)
const (
API_HOST = "https://api.github.com/%s"
)
type User struct {
ID int
Login string
Email string
Name string
RepoCount int `json: "public_repos"`
FollowerCount int `json: "followers"`
}
func (u *User) String() string {
return fmt.Sprintf("[%d] %s", u.ID, u.Login)
}
func GetUser(username string) (*User, error) {
url := fmt.Sprintf(API_HOST, fmt.Sprintf("users/%s", username))
res, err := http.Get(url)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.New(res.Status)
}
user := new(User)
err = json.NewDecoder(res.Body).Decode(user)
if err != nil {
return nil, err
}
return user, nil
}
@ryanlecompte
Copy link

What's this ?!?! I'm seeing more and more gists from you in Go and a lot less in Ruby. :) Making a move?

@leejarvis
Copy link
Author

Odd I didn't get an email from your comment. Na not making a move I still do lots of Ruby for the day job but I've always played with other languages in my spare time. Figured Go was my next victim. Probably going to use it at work now, too :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment