Use Go to get a list of repositories from GitHub

In this article, I will only use Go to ask for GitHub API information about public repositories.net/httpThe stdlib package does not have any other libraries.

GitHub has a nice public API called Search, you can check it at the following locationhttps://developer.github.com/v3/search/#search-repositories.

I am particularly interestedGo repository with more than 10,000 starsAt this point in time.

We can pass queries to it to get the exact information we want. Looking at the Search API documentation, we will have to issue an HTTP GET request tohttps://api.github.com/search/repositoriesPass-through query?q=stars:>=10000+language:go&sort=stars&order=desc: Provide me with a repository of more than 10,000 stars, using the Go language, please sort them by the number of stars.

The simplest code snippet: we usehttp.GetFromnet/httpWe read all the content returned by usingioutil.ReadAll:

package main

import ( “io/ioutil” “log” “net/http” )

func main() { res, err := http.Get(https://api.github.com/search/repositories?q=stars:>=10000+language:go&sort=stars&order=desc”)

<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
	<span style="color:#a6e22e">log</span>.<span style="color:#a6e22e">Fatal</span>(<span style="color:#a6e22e">err</span>)
}

<span style="color:#a6e22e">body</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">ioutil</span>.<span style="color:#a6e22e">ReadAll</span>(<span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">Body</span>)
<span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">Body</span>.<span style="color:#a6e22e">Close</span>()
<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
	<span style="color:#a6e22e">log</span>.<span style="color:#a6e22e">Fatal</span>(<span style="color:#a6e22e">err</span>)
}
<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">StatusCode</span> <span style="color:#f92672">!=</span> <span style="color:#ae81ff">200</span> {
	<span style="color:#a6e22e">log</span>.<span style="color:#a6e22e">Fatal</span>(<span style="color:#e6db74">"Unexpected status code"</span>, <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">StatusCode</span>)
}

<span style="color:#a6e22e">log</span>.<span style="color:#a6e22e">Printf</span>(<span style="color:#e6db74">"Body: %s\n"</span>, <span style="color:#a6e22e">body</span>)

}

it isNot very readable, as you can see. You can paste the content usedOnline formatterUnderstand the actual structure.

Now let's process the JSON and generate a beautiful table from it.

If you are not familiar with the subject of JSON, please refer to the preliminary guide on the following:How to process JSON with Go.

package main

import ( “encoding/json” “fmt” “io/ioutil” “log” “net/http” “os” “text/tabwriter” “time” )

// Owner is the repository owner type Owner struct { Login string }

// Item is the single repository data structure type Item struct { ID int Name string FullName string json:"full_name" Owner Owner Description string CreatedAt string json:"created_at" StargazersCount int json:"stargazers_count" }

// JSONData contains the GitHub API response type JSONData struct { Count int json:"total_count" Items []Item }

func main() { res, err := http.Get(https://api.github.com/search/repositories?q=stars:>=10000+language:go&sort=stars&order=desc”) if err != nil { log.Fatal(err) } body, err := ioutil.ReadAll(res.Body) res.Body.Close() if err != nil { log.Fatal(err) } if res.StatusCode != http.StatusOK { log.Fatal(“Unexpected status code”, res.StatusCode) } data := JSONData{} err = json.Unmarshal(body, &data) if err != nil { log.Fatal(err) } printData(data) }

func printData(data JSONData) { log.Printf(“Repositories found: %d”, data.Count) const format = “%v\t%v\t%v\t%v\t\n” tw := new(tabwriter.Writer).Init(os.Stdout, 0, 8, 2, ’ ', 0) fmt.Fprintf(tw, format, “Repository”, “Stars”, “Created at”, “Description”) fmt.Fprintf(tw, format, “----------”, “-----”, “----------”, “----------”) for _, i := range data.Items { desc := i.Description if len(desc) > 50 { desc = string(desc[:50]) + “…” } t, err := time.Parse(time.RFC3339, i.CreatedAt) if err != nil { log.Fatal(err) } fmt.Fprintf(tw, format, i.FullName, i.StargazersCount, t.Year(), desc) } tw.Flush() }

The above code creates 3 structures to unmarshal the JSON provided by GitHub.JSONDataIs the main containerItemsIs a pieceItem, Repository structure and internal projects,OwnerContains repository owner data.

Give an HTTP responseresBack fromhttp.GetWe use extract bodyres.BodyWe read allbodywithioutil.ReadAll.

After inspectionres.StatusCodemeets thehttp.StatusOKConstant (corresponds to200), we use to unmarshal JSONjson.UnmarshalEnter ourJSONDatastructuredataAnd pass it to us for printingprintDatastructure.

insideprintDataI instantiate atabwriter.Writer, And then process and format the JSON data accordingly to output a beautiful table layout:


More tutorials: