Last active
August 29, 2015 14:06
-
-
Save andylind/19e90fdbb0b883c478b6 to your computer and use it in GitHub Desktop.
Go command line app to get StackOverflow User Rep by Id
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" | |
"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