douyou2732 2015-06-10 20:24
浏览 38
已采纳

使用go-github打印出存储库版本

I am very new to Go and am having issues printing out all releases for a given repo using the go-github api.

I am adapting my code from the example in the project here.

This is the code I have so far.

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
", err)
} else {
    for _, release := range releases {
        fmt.Printf("%v
", release)
    }
}

This works and runs okay (no errors at least), but when I run it, the code doesn't return anything. I have a feeling I am missing something simple but am stuck scratching my head.

  • 写回答

1条回答 默认 最新

  • dongshi1207 2015-06-10 20:37
    关注

    From the GitHub documentation on the list releases API:

    This returns a list of releases, which does not include regular Git tags that have not been associated with a release. To get a list of Git tags, use the Repository Tags API.

    Since the repository you specified contains only tags and not releases, nothing is returned.

    Changing ListReleases to ListTags yields output similar to the following:

    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}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?