【问题标题】:how to use go-github?如何使用go-github?
【发布时间】:2023-01-14 07:34:25
【问题描述】:

我想使用 https://github.com/google/go-github 来评论问题,但是这个测试代码:

package main

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

func main() {
    ctx := context.Background()
    ts := oauth2.StaticTokenSource(
        &oauth2.Token{AccessToken: "token_here"},
    )
    tc := oauth2.NewClient(ctx, ts)

    client := github.NewClient(tc)

    // list all repositories for the authenticated user
    repos, _, err := client.Repositories.List(ctx, "", nil)
}

但我刚开始

# command-line-arguments
./main.go:9:9: undefined: context
./main.go:18:2: repos declared but not used
./main.go:18:12: err declared but not used

背部... 那么 - 我必须做些什么才能使它正常工作以及如何(通过我的令牌)向 github 上的问题发送评论?

多谢!

我测试了来自 https://github.com/google/go-github 的示例代码,但我失败了,因为我对编程和 Go 太陌生了。

【问题讨论】:

    标签: go github-api go-github


    【解决方案1】:
    ./main.go:9:9: undefined: context
    

    您需要导入"context"包才能调用context.Background()

    ./main.go:18:2: repos declared but not used
    ./main.go:18:12: err declared but not used
    

    在调用 client.Repositories.List(ctx, "", nil) 后,您创建了 2 个新变量:reposerr,但从未在任何地方使用过它们。在 Go 中,未使用的变量是编译器错误,因此要么删除这些变量,要么最好按您的意愿使用它们。

    那么 - 我必须做些什么才能使它正常工作以及如何(通过我的令牌)向 github 上的问题发送评论?

    要使用 Github API,您需要给自己一个 access token,并用它替换 "token_here"。然后您可以执行以下操作:

    comment := &github.IssueComment{
        Body: github.String("Hello, world!"),
    }
    comment, _, err := client.Issues.CreateComment(context.Background(), "OWNER", "REPO", ISSUE_NUMBER, comment)
    if err != nil {
        // handle any errors
    }
    

    ... 其中 OWNER 是存储库的所有者,REPO 是存储库的名称,ISSUE_NUMBER 是您要在其中写评论的问题的编号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      • 2022-01-20
      • 2018-05-16
      相关资源
      最近更新 更多