
php小编草莓为大家带来了一篇关于如何使用 go-github 对 Github 问题发表评论的指南。Go-github 是一个强大的 Go 语言库,可以方便地与 Github 进行交互。在本指南中,我们将详细介绍如何使用 go-github 库来发表评论,帮助开发者更好地参与到 Github 社区中。无论是给他人的项目提供反馈还是与其他开发者进行讨论,使用 go-github 发表评论将会变得简单而高效。让我们一起来了解这个过程吧!
我想使用 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 上的问题发送评论(通过我的令牌)?
./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 个新变量:repos 和 err,但从未在任何地方使用过它们。在 go 中,未使用的变量会导致编译器错误,因此要么删除这些变量,要么最好按照您的意愿使用它们。
那么 - 我必须做什么才能使其正常工作以及如何向 github 上的问题发送评论(通过我的令牌)?
要使用 github api,您需要获取一个访问令牌,并替换 “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 是您要在其中写入评论的问题编号。
以上就是如何使用 go-github 对 Github 问题发表评论?的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号