You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过go get从本地裸Git仓库获取Go依赖?

可以通过go get从本地Git裸仓库拉取依赖,问题出在配置细节上

核心问题原因

你的错误是因为Go在解析模块路径时,默认会尝试访问https://git.localhost获取go-get元数据,即便配置了Git的insteadOf,如果GOPRIVATE配置不规范,Go仍会发起HTTPS请求,而git.localhost未在本地DNS解析。

修复步骤

1. 规范GOPRIVATE配置

让Go跳过对git.localhost域名的元数据抓取和代理,直接使用本地Git仓库:

export GOPRIVATE=git.localhost

(无需加通配符*,GOPRIVATE按域名级别匹配)

2. 完善Git的insteadOf规则

覆盖Git可能使用的git://和https://两种协议,确保所有指向git.localhost的请求都转向本地裸仓库路径:

git config --global url."file:///tmp/git/".insteadOf "git://git.localhost/"
git config --global url."file:///tmp/git/".insteadOf "https://git.localhost/"

3. 确保Git允许file协议

git config --global protocol.file.allow always

4. 重新执行go get

进入你的客户端项目目录:

cd /tmp/go-proj
go get git.localhost/repo1

验证依赖是否拉取成功

执行完成后,查看go.mod文件,应该会出现类似以下内容:

require git.localhost/repo1 v0.0.0-xxxxxxxxx-xxxxxxxxxx // indirect

此时即可在代码中正常导入git.localhost/repo1包。

可选补充:解决DNS解析问题(替代GOPRIVATE)

如果不想依赖GOPRIVATE,可修改本地/etc/hosts文件,添加:

127.0.0.1 git.localhost

但此方法需要额外搭建本地HTTP服务提供go-get元数据,不如GOPRIVATE方案简便。

内容的提问来源于stack exchange,提问作者Kirill

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 20:22:46