我是靠谱客的博主 坚强老鼠,这篇文章主要介绍Go使用Private仓库用作module依赖,现在分享给大家,希望可以做个参考。

在go 项目中使用private仓库作为module依赖时,执行get报错

$ go get -x gitee.com/xxx/xx/pkg/logger
......
go: module gitee.com/xxx/xx/pkg/conf/env: git ls-remote -q origin in /Users/xx/go/pkg/mod/cache/vcs/2e35dcd54deffa37be1a6dc6bafd375ae8f1dbb041676b2e1d5c781d4c76627e: exit status 128:
	fatal: could not read Username for 'https://gitee.com': terminal prompts disabled
Confirm the import path was entered correctly.
If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.

解决方案

# 配置go环境变量,设置GOPRIVATE后GONOPROXY和GONOSUMDB会跟着改
go env -w GOPRIVATE="gitee.com"
# 查看go环境变量
$ go env

首先private仓库你有权限访问,并且已经配置公钥,可以操作clone git@gitee.com:xxxx.git
默认go mod是通过git的http/https拉取仓库代码的(上面报错信息可见),需要改成git方式

# 修改要加载仓库的拉取方式
git config --global url."git@gitee.com:xxx/xx.git".insteadof "https://gitee.com/xxx/xx.git"
# 可查看url项已经添加
cat ~/.gitconfig
[user]
        name = xx
        email = xxx@163.com
[url "git@gitee.com:xxx/xx.git"]
        insteadof = https://gitee.com/xxx/xx.git

Docker部署

部署的时候因为没有本地git环境,还需要改一下

...
RUN go env -w GOPRIVATE="gitee.com"
RUN git config --global url."https://$登陆名:$access_token@gitee.com".insteadof "https://gitee.com"
RUN go mod download
RUN CGO_ENABLED=0 go build -o app main.go
...

最后

以上就是坚强老鼠最近收集整理的关于Go使用Private仓库用作module依赖的全部内容,更多相关Go使用Private仓库用作module依赖内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部