In this article, I will only use Go to ask for GitHub API information about public repositories.net/http
The 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/repositories
Pass-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.Get
Fromnet/http
We 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.JSONData
Is the main containerItems
Is a pieceItem
, Repository structure and internal projects,Owner
Contains repository owner data.
Give an HTTP responseres
Back fromhttp.Get
We use extract bodyres.Body
We read allbody
withioutil.ReadAll
.
After inspectionres.StatusCode
meets thehttp.StatusOK
Constant (corresponds to200
), we use to unmarshal JSONjson.Unmarshal
Enter ourJSONData
structuredata
And pass it to us for printingprintData
structure.
insideprintData
I instantiate atabwriter.Writer
, And then process and format the JSON data accordingly to output a beautiful table layout:
More tutorials:
- Use NGINX reverse proxy service Go service
- Copy structure in Go
- Basics of Go web server
- Sorting map types in Go
- In a nutshell
- Go to label description
- Start date and time format
- Use Go for JSON processing
- Variadic function
- Cheat sheet
- Go to the empty interface description
- Use VS Code and Delve to debug Go
- Named Go return parameter
- Generate random numbers and strings in Go
- File system structure of Go project
- Binary search algorithm in Go
- Use command line flags in Go
- GOPATH explained
- Use Go to build a command line application: lolcat
- Use Go to build CLI commands: Cowsay
- Use shell and tube in Go
- Go CLI Tutorial: Fortune Clone
- Use Go to list files in a folder
- Use Go to get a list of repositories from GitHub
- Go, append a short string to the file
- Go, convert the string to byte slices
- Use Go to visualize your local Git contributions
- Getting started with Go CPU and memory analysis
- Solve the "Does not support index" error in the Go program
- Measuring the execution time in the Go program
- Use Go to build a web crawler to detect duplicate titles
- Best Practice: Pointer or Value Receiver?
- Best practice: Should you use methods or functions?
- Go data structure: set
- Go to the map cheat sheet
- Generating the implementation of generic types in Go
- Go data structure: dictionary
- Go data structure: hash table
- Implement event listeners in "through the channel"
- Go data structure: stack
- Go data structure: queue
- Go data structure: binary search tree
- Go data structure: graphics
- Go data structure: linked list
- A complete guide to Go data structures
- Compare Go value
- Is Go object-oriented?
- Use SQL database in Go
- Use environment variables in Go
- Last tutorial: REST API supported by PostgreSQL
- Enable CORS on the Go web server
- Deploy Go application in Docker container
- Why Go is a powerful language to learn as a PHP developer
- Go and delete the io.Reader.ReadString newline
- To start, how to watch the changes and rebuild the program
- To count the months since the date
- Access HTTP POST parameters in Go