登录
首页 >  Golang >  Go问答

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

来源:stackoverflow

时间:2024-03-30 21:03:32 215浏览 收藏

IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《在 golang 中导入私有仓库时版本和 https 引用无效》,聊聊,我们一起来看看吧!

问题内容

尝试在 golang 中将私有存储库作为包导入。做了:

git config --global [email protected]:.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/[email protected]: verifying module: github.com/XXX/[email protected]: reading https://sum.golang.org/lookup/github.com/!X!X!X/[email protected]: 410 Gone
        server response:
        not found: github.com/XXX/[email protected]: 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 的引用。我还能做什么?


解决方案


可以肯定的是,我更喜欢在 git config 命令中使用引号:

git config --global url."[email protected]:".insteadof "https://github.com/"

请参阅此 gist as an example

它包括:

使用 [email protected] 的替代方法是在您的 github 帐户上生成个人访问令牌,授予其存储库访问权限,然后使用以下命令:

git config --global url."https://${github_token}:[email protected]/".insteadof "https://github.com/"

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

为了能够使用 go 1.18 拉取我的私有模块,我必须做两件事:

  1. 相当于 @vonc 的答案,将以下两行添加到我的 ~/.gitconfig 中:
[url "ssh://[email protected]/"]
        insteadof = https://github.com/
  1. 将我的私有模块 git 路径的每个显式添加到 goprivate 变量:
go env -w GOPRIVATE="github.com/projname/mod1,github.com/projname/mod2,github.com/projname/mod3"

我认为这是必要且充分的,或者至少是充分的。

今天关于《在 golang 中导入私有仓库时版本和 https 引用无效》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>