2015-06-10 23 views
0

我對Go非常陌生,並且在使用go-github api打印出給定回購的所有版本時遇到問題。使用go-github打印出存放庫版本

我在這裏調整我的代碼從example in the project

這是我到目前爲止的代碼。

package main 

import (
    "fmt" 
    "github.com/google/go-github/github" 
    "golang.org/x/oauth2" 
) 

func main() { 

// authentication 
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: "XXX"}) 
tc := oauth2.NewClient(oauth2.NoContext, ts) 

client := github.NewClient(tc) 

// list all releases for single repo 
fmt.Println("Releases for repo") 
opt := &github.ListOptions{Page: 2, PerPage: 10} 
releases, _, err := client.Repositories.ListReleases("hashicorp", "terraform", opt) 

if err != nil { 
    fmt.Printf("error: %v\n", err) 
} else { 
    for _, release := range releases { 
     fmt.Printf("%v\n", release) 
    } 
} 

這工作和運行好(至少沒有錯誤),但是當我運行它,代碼不會返回任何東西。我有一種感覺,我錯過了一些簡單的東西,但我一直在抓我的頭。

回答

2

GitHub documentation on the list releases API

這將返回發佈的名單,其中不包括尚未與發佈相關的規則的Git標籤。要獲取Git標籤列表,請使用Repository Tags API

由於您指定的存儲庫僅包含標籤而不包含任何版本,因此不會返回任何內容。

更改ListReleasesListTags產生類似下面的輸出:

Releases for repo 
{0xc2081e1a10 github.Commit{SHA:"d8292227960eab405863ffbc5d13cc85839aa2db", URL:"https://api.github.com/repos/hashicorp/terraform/commits/d8292227960eab405863ffbc5d13cc85839aa2db"} 0xc2081e1a50 0xc2081e1a60} 
{0xc2081e1c40 github.Commit{SHA:"4ed2c8d956d2eae83a2a3750290711ce7fa1ec76", URL:"https://api.github.com/repos/hashicorp/terraform/commits/4ed2c8d956d2eae83a2a3750290711ce7fa1ec76"} 0xc2081e1c60 0xc2081e1c70} 
{0xc2081e1ca0 github.Commit{SHA:"33e11f0eba1b634849784151b29f69314bfae827", URL:"https://api.github.com/repos/hashicorp/terraform/commits/33e11f0eba1b634849784151b29f69314bfae827"} 0xc2081e1cb0 0xc2081e1cc0} 
{0xc2081e1cf0 github.Commit{SHA:"8175d7f82134d378234a407e529853da681af07e", URL:"https://api.github.com/repos/hashicorp/terraform/commits/8175d7f82134d378234a407e529853da681af07e"} 0xc2081e1d10 0xc2081e1d20} 
{0xc2081e1d50 github.Commit{SHA:"166efa1ba0c7aa2fb5a7230f787c8499c765786e", URL:"https://api.github.com/repos/hashicorp/terraform/commits/166efa1ba0c7aa2fb5a7230f787c8499c765786e"} 0xc2081e1d60 0xc2081e1d70} 
{0xc2081e1da0 github.Commit{SHA:"1894e985557f010a4bca873c5b10bedb1cd7de5d", URL:"https://api.github.com/repos/hashicorp/terraform/commits/1894e985557f010a4bca873c5b10bedb1cd7de5d"} 0xc2081e1dc0 0xc2081e1dd0} 
{0xc2081e1e00 github.Commit{SHA:"0bc0c03fece07c4f21ee5195743116a4d418f234", URL:"https://api.github.com/repos/hashicorp/terraform/commits/0bc0c03fece07c4f21ee5195743116a4d418f234"} 0xc2081e1e10 0xc2081e1e20} 
{0xc2081e1e50 github.Commit{SHA:"4e11ed4327fa638a4c2587489a8660c69beb4047", URL:"https://api.github.com/repos/hashicorp/terraform/commits/4e11ed4327fa638a4c2587489a8660c69beb4047"} 0xc2081e1e70 0xc2081e1e80} 
{0xc2081e1eb0 github.Commit{SHA:"a4cf3fec1c8341041f41897ea84781e6be9b1dad", URL:"https://api.github.com/repos/hashicorp/terraform/commits/a4cf3fec1c8341041f41897ea84781e6be9b1dad"} 0xc2081e1ec0 0xc2081e1ed0} 
{0xc2081e1f00 github.Commit{SHA:"146d081b65aba3e0f183d6e4b26f3e794fabaebf", URL:"https://api.github.com/repos/hashicorp/terraform/commits/146d081b65aba3e0f183d6e4b26f3e794fabaebf"} 0xc2081e1f20 0xc2081e1f30}