Created
August 8, 2012 14:56
-
-
Save leejarvis/3295636 to your computer and use it in GitHub Desktop.
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 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 | |
} |
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
What's this ?!?! I'm seeing more and more gists from you in Go and a lot less in Ruby. :) Making a move?