搜尋
首頁後端開發Golang如何使用 go-github 對 Github 問題發表評論?

如何使用 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。如有侵權,請聯絡admin@php.cn刪除
去其他語言:比較分析去其他語言:比較分析Apr 28, 2025 am 12:17 AM

goisastrongchoiceforprojectsneedingsimplicity,績效和引發性,butitmaylackinadvancedfeatures and ecosystemmaturity.1)

比較以其他語言的靜態初始化器中的初始化功能比較以其他語言的靜態初始化器中的初始化功能Apr 28, 2025 am 12:16 AM

Go'sinitfunctionandJava'sstaticinitializersbothservetosetupenvironmentsbeforethemainfunction,buttheydifferinexecutionandcontrol.Go'sinitissimpleandautomatic,suitableforbasicsetupsbutcanleadtocomplexityifoverused.Java'sstaticinitializersoffermorecontr

GO中初始功能的常見用例GO中初始功能的常見用例Apr 28, 2025 am 12:13 AM

thecommonusecasesfortheinitfunctionoare:1)加載configurationfilesbeforeThemainProgramStarts,2)初始化的globalvariables和3)runningpre-checkSorvalidationsbeforEtheprofforeTheProgrecce.TheInitFunctionIsautefunctionIsautomentycalomationalmatomatimationalycalmatemationalcalledbebeforethemainfuniinfuninfuntuntion

GO中的頻道:掌握際際交流GO中的頻道:掌握際際交流Apr 28, 2025 am 12:04 AM

ChannelsarecrucialingoforenablingsafeandefficityCommunicationBetnewengoroutines.theyfacilitateSynChronizationAndManageGoroutIneLifeCycle,EssentialforConcurrentProgramming.ChannelSallSallSallSallSallowSallowsAllowsEnderDendingAndReceivingValues,ActassignalsignalsforsynChronization,and actassignalsynChronization and andsupppor

包裝錯誤:將上下文添加到錯誤鏈中包裝錯誤:將上下文添加到錯誤鏈中Apr 28, 2025 am 12:02 AM

在Go中,可以通過errors.Wrap和errors.Unwrap方法來包裝錯誤並添加上下文。 1)使用errors包的新功能,可以在錯誤傳播過程中添加上下文信息。 2)通過fmt.Errorf和%w包裝錯誤,幫助定位問題。 3)自定義錯誤類型可以創建更具語義化的錯誤,增強錯誤處理的表達能力。

使用GO開發時的安全考慮使用GO開發時的安全考慮Apr 27, 2025 am 12:18 AM

Gooffersrobustfeaturesforsecurecoding,butdevelopersmustimplementsecuritybestpracticeseffectively.1)UseGo'scryptopackageforsecuredatahandling.2)Manageconcurrencywithsynchronizationprimitivestopreventraceconditions.3)SanitizeexternalinputstoavoidSQLinj

了解GO的錯誤接口了解GO的錯誤接口Apr 27, 2025 am 12:16 AM

Go的錯誤接口定義為typeerrorinterface{Error()string},允許任何實現Error()方法的類型被視為錯誤。使用步驟如下:1.基本檢查和記錄錯誤,例如iferr!=nil{log.Printf("Anerroroccurred:%v",err)return}。 2.創建自定義錯誤類型以提供更多信息,如typeMyErrorstruct{MsgstringDetailstring}。 3.使用錯誤包裝(自Go1.13起)來添加上下文而不丟失原始錯誤信息,

並發程序中的錯誤處理並發程序中的錯誤處理Apr 27, 2025 am 12:13 AM

對效率的Handleerrorsinconcurrentgopragrs,UsechannelstocommunicateErrors,enplionErrorWatchers,Instertimeout,UsebufferedChannels和Provideclearrormessages.1)USEchannelelStopassErtopassErrorsErtopassErrorsErrorsErrorsFromGoroutInestOthemainFunction.2)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器