6
Go GitHub API code snippets and examples
source link: https://kenanbek.github.io/github-api-go-examples
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Go GitHub API code snippets and examples
Published: May 25, 2020 / Last modified: May 25, 2020
Initialization:
ctx := context.TODO()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: "YOUR-ACCESS-TOKEN"},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
List user’s repositories:
repos, _, err := client.Repositories.List(ctx, "kenanbek", nil)
if _, ok := err.(*github.RateLimitError); ok {
log.Println("hit rate limit")
}
for _, repo := range repos {
log.Printf("\t Name: %s \n", *repo.Name)
log.Printf("\t Description: %s \n", *repo.Description)
log.Printf("\t URL: %s \n", *repo.URL)
log.Println()
}
Search repositories by the given term:
result, _, err := client.Search.Repositories(ctx, "web", &github.SearchOptions{})
if _, ok := err.(*github.RateLimitError); ok {
log.Println("hit rate limit")
}
if result != nil {
log.Println("Total results: ", result.Total)
for _, repo := range result.Repositories {
log.Printf("\t Name: %s \n", *repo.Name)
log.Printf("\t Description: %s \n", *repo.Description)
log.Printf("\t URL: %s \n", *repo.URL)
log.Println()
}
}
Search Go developers with more than 100 followers:
opt := &github.SearchOptions{
ListOptions: github.ListOptions{
PerPage: 5,
},
}
result, response, err := client.Search.Users(ctx, "followers:>=100 language:go", opt)
if _, ok := err.(*github.RateLimitError); ok {
log.Println("hit rate limit")
}
if response != nil {
log.Println("Rate Information")
log.Printf("Rate: %v \n", response.Rate)
log.Println()
}
if result != nil {
log.Println("Total results: ", result.GetTotal())
for i, userB := range result.Users {
user, _, _ := client.Users.Get(ctx, userB.GetLogin())
log.Printf("\t %d \n", i)
log.Printf("\t ID: %d \n", user.GetID())
log.Printf("\t Login: %s \n", user.GetLogin())
log.Printf("\t Created: %v \n", user.GetCreatedAt())
log.Printf("\t Followers: %d \n", user.GetFollowers())
log.Println()
}
}
[ Tags ]
github
api
library
examples
snippets
go
golang
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK