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

如何通过SSH从私有Git仓库导入Go模块?

解决私有Go模块通过SSH使用go get的问题

错误原因分析

  1. 第一个命令报错:go get ssh://<user>@<servername>.local/var/git_repos/tftp
    Go的模块路径不支持直接以ssh://作为前缀,模块路径要求遵循「域名/路径」的格式,因此直接传入SSH URL会被判定为格式错误。

  2. 第二个命令报错:go get <servername>.local/var/git_repos/tftp
    Go默认会对模块路径发起HTTPS请求获取go-get=1元数据,但你的私有仓库未提供HTTPS服务;同时未配置GOPRIVATE告知Go该域名下是私有模块,导致Go尝试公共HTTPS拉取失败。

解决步骤

1. 确保私有仓库的模块命名正确

进入你的tftp仓库目录,检查go.mod文件的第一行是否为:

module <servername>.local/var/git_repos/tftp

如果没有该文件,执行以下命令初始化模块并提交到Git:

go mod init <servername>.local/var/git_repos/tftp
git add go.mod
git commit -m "init go module"
git push origin main

2. 配置GOPRIVATE环境变量

告诉Go<servername>.local域名下的模块是私有,无需走公共代理拉取:

  • 临时生效(当前终端会话):
    export GOPRIVATE=<servername>.local
    
  • 永久生效(以bash为例,其他shell对应修改配置文件):
    echo 'export GOPRIVATE=<servername>.local' >> ~/.bashrc
    source ~/.bashrc
    

3. 修正Git的insteadOf配置

确保.gitconfig中的配置匹配你的服务器地址,替换<user>和<servername>为实际值:

[url "ssh://<user>@<servername>.local/"]
    insteadOf = https://<servername>.local/

如果你的仓库都集中在/var/git_repos/路径下,也可以更精准配置:

[url "ssh://<user>@<servername>.local/var/git_repos/"]
    insteadOf = https://<servername>.local/var/git_repos/

4. 执行正确的go get命令

现在直接使用模块路径执行go get即可:

go get <servername>.local/var/git_repos/tftp
# 或者指定版本,比如最新版
go get <servername>.local/var/git_repos/tftp@latest

内容的提问来源于stack exchange,提问作者Darwin von Corax

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 06:35:10