如何设定 GOPRIVATE 环境变量

我开始工作的 Go项目,它使用一些私人模块从 Github 私人回购和每当我尝试运行 go run main.go它给我一个以下的 410 Gone错误:

验证 github.com/repourl/go-proto@v2.86.0+incompatible/go.mod : github.com/repourl/go-proto@v2.86.0+incompatible/go.mod : 阅读 abc 0:410

我可以很容易地从终端克隆私人回购,这意味着我的 ssh键配置正确。我读了 给你,我需要设置 GOPRIVATE的环境变量,但我不知道如何做到这一点。

有人能回答或指出相关的教程吗?

Go: v1.13,操作系统: macOS Mojave

102994 次浏览

Short Answer:

go env -w GOPRIVATE=github.com/repoURL/private-repo

OR

If you want to allow all private repos from your organization

go env -w GOPRIVATE=github.com/<OrgNameHere>/*

Long Answer:

Check "Module configuration for non-public modules" for more information:

The GOPRIVATE environment variable controls which modules the go command considers to be private (not available publicly) and should therefore not use the proxy or checksum database. The variable is a comma-separated list of glob patterns (in the syntax of Go's path.Match) of module path prefixes. For example,

 GOPRIVATE=*.corp.example.com,rsc.io/private

causes the go command to treat as private any module with a path prefix matching either pattern, including git.corp.example.com/xyzzy, rsc.io/private, and rsc.io/private/quux.

. .

The 'go env -w' command (see 'go help env') can be used to set these variables for future go command invocations.


Note on the usage of ssh:

If you use ssh to access git repo (locally hosted), you might want to add the following to your ~/.gitconfig:

[url "ssh://git@git.local.intranet/"]
insteadOf = https://git.local.intranet/

for the go commands to be able to access the git server.

Just a follow up on the usage of ssh, this is the command used to get it working:

GitHub:

git config --global url."git@github.com:".insteadOf "https://github.com/"

Bitbucket:

git config --global url."git@bitbucket.org:".insteadOf "https://bitbucket.org/"

If zsh use:

go env -w GOPRIVATE='gitlab.my_firm_name.com/*'

otherwise get

zsh: no matches found: GOPRIVATE=gitlab.my_firm_name.com/*