私有库
go 的 module 放在私有仓库中,是非常常见的事情,如果需要从这些库中克隆,需要修改一些 go env。假定,私有库地址为 git.yourdomain.com 。
下面是配置私有库认证走 ssh 的方式。
go env -w GOPRIVATE=git.yourdomain.com/*
go env -w GONOSUMDB=git.yourdomain.com/*
# 注意: 这里是 https 还是 http 要看私有库的访问方式
git config --global url."git@git.yourdomain.com:".insteadOf "https://git.yourdomain.com/"subgroup module 无法下载
在感觉一切万事俱备之后,仍然无法下载,绞尽脑汁也想不清楚哪里的问题。
go: git.yourdomain.com/group/subgroup/subgroup/yourmodule imports
git.yourdomain.com/group/subgroup/subgroup/yourmodule: reading git.yourdomain.com/group/subgroup/subgroup/yourmodule/go.mod at revis
ion subgroup/yourmodule/v0.2.0: git ls-remote -q https://git.yourdomain.com/group/subgroup.git in /data/zhenkai.sun/.asdf/installs/golang/1.25.4/p
ackages/pkg/mod/cache/vcs/3437b85fa083a2dc69315bb2ad1d43fc8f237a4bc8f4a893bfc88009cb939389: exit status 128:
remote:
remote: ========================================================================
remote:
remote: The project you were looking for could not be found or you don't have permission to view it.
remote:
remote: ========================================================================
remote:
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.如果尝试使用 ssh 直接克隆库也是没问题的,是不是很费解。
原因在这里。
# 我们期望的 module
https://git.yourdomain.com/group/subgroup/subgroup/yourmodule
# go module 尝试下载的
https://git.yourdomain.com/group/subgroup.git是不是可以看到问题,go module 把 subgroup 当成 module 了,自然找不到 go.mod。
解决方案
方案一 目前可以在 go.mod 中使用 replace 的方式来解决。
replace(
git.yourdomain.com/group/subgroup/subgroup/yourmodule => git.yourdomain.com/group/subgroup/subgroup/yourmodule.git v0.2.0
)方案二 module name 添加 .git 后缀
这个方案需要修改依赖库,不仅要改 go.mod,还有替换所有引入。
git.yourdomain.com/group/subgroup/subgroup/yourmodule -> git.yourdomain.com/group/subgroup/subgroup/yourmodule.git