通常为了下载依赖,会设置GOPROXY。其中私有仓库需要加入到GOPRIVATE中,避免被代理。

go env -w GOPROXY=https://goproxy.cn
go env -w GOPRIVATE=gitlab.pintechs.com/*
go env -w GONOPROXY=gitlab.pintechs.com

在给仓库添加ssh公钥后,内部仓库比如gitlab.pintechs.com/eh/print-server,可以git clone下来,但是go get和 go mod tidy都不行,报这个错:

$ go build main.go
go: downloading gitlab.pintechs.com/eh/print-server v0.0.0-20240229074657-3b36e3eb10a8
models/admin/printserver.go:14:2: gitlab.pintechs.com/eh/print-server@v0.0.0-20240229074657-3b36e3eb10a8: invalid version: git ls-remote -q origin in /home/chris/go/pkg/mod/cache/vcs/9335763a304c55f76637ae8f094e1516de9a7d8b16bfc22c040a6f764601fec7: exit status 128:
         git@gitlab.pintechs.com: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
        fatal: Could not read from remote repository.

        Please make sure you have the correct access rights
        and the repository exists.

可能是证书问题,即使是安装证书后,也无法通过鉴权。
解决方法是,

  1. 安装证书
  2. 修改git的设置,通过 SSH 而不是 HTTPS 来访问 Git 仓库。
git config --global --add url."git@gitlab.pintechs.com:".insteadOf "https://gitlab.pintechs.com/"

运行git config --global --list查看配置:

$ git config --global  --list
url.git@gitlab.pintechs.com:.insteadof=https://gitlab.pintechs.com/
http.sslverify=true
user.name=chris
user.email=chajiuqqq@gmail.com

最后运行go mod tidy同步即可。