Skip to content

Instantly share code, notes, and snippets.

@plutov
Last active May 4, 2020 19:43
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 plutov/932e1e9315012a65031bf41dfd9c21ee to your computer and use it in GitHub Desktop.
Save plutov/932e1e9315012a65031bf41dfd9c21ee to your computer and use it in GitHub Desktop.
testable4.go
type Repo struct {
StargazersCount int `json:"stargazers_count"`
}
func GetAverageStarsPerRepo(username string) (float64, error) {
res, err := http.Get(fmt.Sprintf("https://api.github.com/users/%s/repos", username))
if err != nil {
return 0, err
}
defer res.Body.Close()
repos := []Repo{}
if err := json.NewDecoder(res.Body).Decode(&repos); err != nil {
return 0, err
}
if len(repos) == 0 {
return 0, nil
}
var total int
for _, r := range repos {
total += r.StargazersCount
}
return float64(total) / float64(len(repos)), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment