Last active
May 4, 2020 19:43
-
-
Save plutov/932e1e9315012a65031bf41dfd9c21ee to your computer and use it in GitHub Desktop.
testable4.go
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
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