首頁  >  文章  >  後端開發  >  講解go get如何安裝gitlab私有包

講解go get如何安裝gitlab私有包

藏色散人
藏色散人轉載
2022-01-06 15:29:163463瀏覽

本文由golang教學欄位來總結介紹go get怎麼安裝gitlab私有包,希望對需要的朋友有幫助!

  • 取得gitlab的token

    #進入Gitlab—>Settings—>Access Tokens,然後建立一個personal access token,這裡權限最好選擇只讀(read_repository )。

  • git配置添加access token

有了access token後,我們還需要在git中進行配置,這樣才能go get下了私有倉庫的包,需要把剛剛的token加進git的請求頭中,操作如下:

git config --global http.extraheader "PRIVATE-TOKEN: YOUR_PRIVATE_TOKEN"
  • 配置git將請求從ssh轉換為http
git config --global url."git@gitlab_url:groupName/projectName.git".insteadOf "https://gitlab_url/groupName/projectName.git"`

#上面是透過指令修改配置,也可以像這樣,直接修改設定檔~/.gitconfig,在裡面加入以下設定:

[url "git@{{gitlab_url}}:"]
        insteadOf = https://{{gitlab_url}}/

注意:insteadof 中的參數是https,因為不管目標位址是http還是https,go get預設使用https方式訪問,因此我們要強制將https轉換成git協議

  • 如果這樣還拉不下來,就需要對go的一些環境變量進行設定

Bash (Liunx or macOS)

# 配置 GOPROXY 环境变量
export GOPROXY=https://goproxy.io,direct

# 还可以设置不走 proxy 的私有仓库或组,多个用逗号相隔(可选)
export GOPRIVATE=git.mycompany.com,github.com/my/private

Powerbash (Windows)

# 配置 GOPROXY 环境变量
$env:GOPROXY = "https://goproxy.io,direct"

# 还可以设置不走 proxy 的私有仓库或组,多个用逗号相隔(可选)
$env:GOPRIVATE = "git.mycompany.com,github.com/my/private"

以上是講解go get如何安裝gitlab私有包的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:learnku.com。如有侵權,請聯絡admin@php.cn刪除