转到 Mod 私人回购

Go Mod Private Repo

所以我有一个我的 main.go 导入的私人仓库。我在执行 go build 时遇到此错误:

cannot find module for path

我需要为私人仓库做任何特别的事情吗?我一直在谷歌搜索,找不到任何好的信息。它适用于 dep.

您应该使用 SSH 密钥来获取您的存储库,检查您的 SSH 密钥是否也在系统钥匙串中:

ssh-add -K ~/.ssh/id_rsa

鉴于这样的私人仓库经常处于积极开发中,我个人只是将其克隆到我的 $GOPATH 中的 "proper" 位置并使用源代码管理(例如 git)就像任何其他项目一样。在 Rodrigo 的回答中添加 SSH 密钥很好,但是如果您正在积极开发私人仓库,那么将其克隆到正确目录的额外步骤无论如何都不是困难的步骤 go get它。

因此,例如,对于托管在 Github 上的私人存储库,我会 cd$GOHOME/src/github.com/git-username-for-repo 然后 git clone the-repo

(从 this SO Question 复制的答案)

我在 Medium 上写了一个解决方案:Go Modules with Private Git Repositories

我们处理它的方式与 Alex Pliutau 的回答基本相同,并且该博客通过示例更详细地介绍了如何使用来自 GitHub/GitLab/BitBucket。它还进入了一个工作的 Dockerfile 示例,用于使用具有私有存储库的模块。

GitLab 的相关位:

git config --global \
  url."https://oauth2:${personal_access_token}@privategitlab.com".insteadOf \
  "https://privategitlab.com"

#or 

git config --global \
  url."https://${user}:${personal_access_token}@privategitlab.com".insteadOf \
  "https://privategitlab.com"

希望对您有所帮助。

这样做

git config --global --add url."git@your-repo.com:".insteadOf "https://your-repo.com/"
export GOPRIVATE='your-repo.com'

确保通过 ssh 进行的 git 克隆有效。