首页 > 解决方案 > go get 无法使用不可路由的 IP 地址

问题描述

我在本地网络(192.168.0.12)的计算机上设置了 git repo。它有一个条目/etc/hosts

192.168.0.12    ubuntu-18-extssd

go get 无法将我的主机名 ( ubuntu-18-extssd) 识别为主机名,因此我使用 IP 地址。

现在,当我尝试 go get like this

go get 192.168.0.12/gitrepo/go-package-test-stringutil/stringpackage

它返回错误

package 192.168.0.12/gitrepo/go-package-test-stringutil/stringpackage: unrecognized import path "192.168.0.12/gitrepo/go-package-test-stringutil/stringpackage": https fetch: Get "https://192.168.0.12/gitrepo/go-package-test-stringutil/stringpackage?go-get=1": dial tcp 192.168.0.12:443: connect: connection refused

所以我告诉 git 使用 ssh 代替:

git config --global url."dean@192.168.0.12:".insteadOf "https://192.168.0.12/"

在我的 ~/.gitconfig 我有

[url "git@github.com:"]
    insteadOf = https://github.com/
[url "dean@192.168.0.12:"]
    insteadOf = https://192.168.0.12/

但是 go get 仍然给出同样的错误。不过,github 的条目有效。

当我告诉它使用 ssh 时,为什么仍然尝试使用 httpsgo get的组合?git

标签: gitgogo-get

解决方案


Go 无法192.168.0.12/gitrepo/go-package-test-stringutil/stringpackage直接推断存储库类型,因此它试图在以下位置查找元标记https://192.168.0.12/gitrepo/go-package-test-stringutil/stringpackage?go-get=1

如果导入路径不是已知的代码托管站点并且也缺少版本控制限定符,则 go 工具会尝试通过 https/http 获取导入并在文档的 HTML 中查找标记。

https://golang.org/cmd/go/#hdr-Remote_import_paths

如果您不想运行返回适当元标记的服务器,则必须通过添加.git到导入路径来让 Go 知道该包位于 git 存储库中。使用以下命令之一,具体取决于您的存储库所在的位置:

go get 192.168.0.12/gitrepo/go-package-test-stringutil/stringpackage.git
go get 192.168.0.12/gitrepo/go-package-test-stringutil.git/stringpackage
go get 192.168.0.12/gitrepo.git/go-package-test-stringutil/stringpackage

推荐阅读