使用 go-github 评论 Github 问题的步骤
时间:2024-02-10
浏览:243
作者:佚名
本篇文章给大家分享《使用 go-github 评论 Github 问题的步骤》,覆盖了Golang的常见基础知识,其实一个语言的全部知识点一篇文章是不可能说完的,但希望通过这些问题,让读者对自己的掌握程度有一定的认识(B 数),从而弥补自己的不足,更好的掌握它。
问题内容
我想使用 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 问题的步骤》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
