首頁 > 後端開發 > Golang > 主體

如何使用 go-github 對 Github 問題發表評論?

WBOY
發布: 2024-02-10 08:30:08
轉載
840 人瀏覽過

如何使用 go-github 对 Github 问题发表评论?

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 個新變數:reposerr,但從未在任何地方使用過它們。在 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中文網其他相關文章!

來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!