Skip to content

Instantly share code, notes, and snippets.

@renanregis
Created September 11, 2018 16:34
Show Gist options
  • Save renanregis/cc4e780535b0020469405efe21289679 to your computer and use it in GitHub Desktop.
Save renanregis/cc4e780535b0020469405efe21289679 to your computer and use it in GitHub Desktop.
listing top 10 go repositories by stars
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
type Item struct {
Name string `json:"name"`
Description string `json:"description"`
URL string `json:"url"`
StargazersCount int `json:"stargazers_count"`
}
type JSONData struct {
Items []Item
}
func main() {
response, err := http.Get("https://api.github.com/search/repositories?q=stars:>=1000+language:go&page=1&per_page=10&sort=stars&order=desc")
if err != nil {
log.Fatal(err)
} else {
body, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
log.Fatal(err)
}
toJSON(string(body))
}
}
func toJSON(str string) {
data := JSONData{}
err := json.Unmarshal([]byte(str), &data)
if err != nil {
log.Fatalln("Cannot unmarshal json blob", err.Error())
}
messageJSON, _ := json.Marshal(data)
err = ioutil.WriteFile("files.json", messageJSON, 0644)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment