Skip to content

Instantly share code, notes, and snippets.

@andylind
Last active August 29, 2015 14:06
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 andylind/19e90fdbb0b883c478b6 to your computer and use it in GitHub Desktop.
Save andylind/19e90fdbb0b883c478b6 to your computer and use it in GitHub Desktop.
Go command line app to get StackOverflow User Rep by Id
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
)
type sofUser struct {
Items []struct {
Display_name string
Reputation int
}
}
// Get a StackOverflow (SOF) user's rep points user id
func main() {
var userId string
flag.StringVar(&userId, "u", "1", "-u=userId")
flag.Parse()
url := "https://api.stackexchange.com/2.2/users/" + userId + "?order=desc&sort=reputation&site=stackoverflow"
var user sofUser
getJsonData(url, &user)
for _, item := range user.Items {
fmt.Printf("SOF Rep for %s = %d\n", item.Display_name, item.Reputation)
}
}
func getJsonData(url string, data interface{}) {
r, _ := http.Get(url)
defer r.Body.Close()
dec := json.NewDecoder(r.Body)
dec.Decode(&data)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment