在 golang 中导入私有 repo 时版本和 https 引用无效

Posted

技术标签:

【中文标题】在 golang 中导入私有 repo 时版本和 https 引用无效【英文标题】:Invalid version and https references when importing private repo in golang 【发布时间】:2020-07-23 21:43:33 【问题描述】:

尝试在 golang 中导入私有 repo 作为包。做到了:

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

所以理论上所有对 https 的引用都会被 ssh 版本取代。

github.com/XXX/util

是我的私人仓库,它是一个 go 模块。

我执行 go get -v 并获取:

[gabriel@xiridio backend]$ go get -v
go: finding module for package github.com/XXX/util
go: downloading github.com/XXX/util v0.0.0-20200411022955-454673685ff5
go: finding module for package github.com/XXX/util
main.go:12:2: github.com/XXX/util@v0.0.0-20200411022955-454673685ff5: verifying module: github.com/XXX/util@v0.0.0-20200411022955-454673685ff5: reading https://sum.golang.org/lookup/github.com/!X!X!X/util@v0.0.0-20200411022955-454673685ff5: 410 Gone
        server response:
        not found: github.com/XXX/util@v0.0.0-20200411022955-454673685ff5: invalid version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in /tmp/gopath/pkg/mod/cache/vcs/f1fdc5cc42a6995f954688df06783c05d28e4a60e9aaf6930a88a2487b913907: exit status 128:
                fatal: could not read Username for 'https://github.com': terminal prompts disabled

看起来存在“版本”问题,而且由于某种原因,仍然存在对 https 的引用。我还能做什么?

【问题讨论】:

【参考方案1】:

为了确定,我更喜欢在 git config 命令中使用引号:

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

看到这个gist as an example。

它包括:

使用git@github.com 的另一种方法是在您的 GitHub 帐户上生成个人访问令牌,授予其存储库访问权限,然后改用以下命令:

git config --global url."https://$GITHUB_TOKEN:x-oauth-basic@github.com/".insteadOf "https://github.com/"

还要检查“go get results in 'terminal prompts disabled' error for GitHub private repo”,其中提到了GOPRIVATE 的使用。

【讨论】:

谢谢。使用 git config 和 GOPRIVATE 工作。 @GabrielA.Zorrilla 您使用了两个git config 中的哪一个? 引号对 ~/.gitconfig 的输出没有任何影响,因此两者都是有效的。

以上是关于在 golang 中导入私有 repo 时版本和 https 引用无效的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Golang 中导入本地包

在 TypeScript 中导入没有作用域名称的作用域 npm 模块

无法在 virtualenv 中导入 django,但在停用时可以工作

如何在 Eclipse 中导入 facebook sdk,版本 4.2.0?

如何在 Symfony 学说实体中导入 Excelsheets

在没有 Ivy 的 Angular 10 库中导入 RouterModule