如何设置 GOPRIVATE 环境变量

How to set GOPRIVATE environment variable

我开始在一个 Go 项目上工作,它使用了一些来自 Github 私人仓库的私人模块,每当我尝试 运行 go run main.go 它给我一个下面的410 Gone 错误:

verifying github.com/repoURL/go-proto@v2.86.0+incompatible/go.mod: github.com/repoURL/go-proto@v2.86.0+incompatible/go.mod: reading https://sum.golang.org/lookup/github.com/!repoURL/go-proto@v2.86.0+incompatible: 410 Gone

我可以轻松地从终端克隆私有存储库,这意味着我的 ssh 密钥配置正确。我读到 here 我需要设置 GOPRIVATE 环境变量,但我不确定该怎么做。

任何人都可以回答或指出相关教程吗?

Go: v1.13, OS: macOS Mojave

简答:

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

如果您想要允许来自您组织的所有私人回购

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

长答案:

查看"Module configuration for non-public modules"了解更多信息:

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.


ssh使用注意事项:

如果您使用 ssh 访问 git 存储库(本地托管),您可能需要将以下内容添加到您的 ~/.gitconfig

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

go 命令能够访问 git 服务器。

只是跟进 ssh 的用法,这是用来让它工作的命令:

GitHub:

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

比特桶:

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

如果 zsh 使用:

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

否则得到

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