首页 > 编程技术 > Golang

完美解决golang go get私有仓库的问题

发布时间:2021-5-4 20:01

解决golang go get gitlab私有仓库的问题(1.13)

1. 问题描述

require ( 
 git.xxxxxxx.com/middle/user v0.0.1
)

go mod tidy 导入包失败

go get git.xxxxxxx.com/middle/user 失败

go build 有CHECKSUM过程,无法编译

2. 现象分析

go get 不支持代码支持之外的仓库。并且git 调用链过程采取了https

下载过程如果机器设置了GOPROXY,会导致下载失败

编译过程会导致CHECKSUM失败

3. 物料

物料 说明
git.xxxxxxx.com 私有仓库
middle/user.git 用户服务模块

方案

1、给释出的仓库打tag比如v0.0.1,这样仓库地址就可以被识别

2、export GOPRIVATE=git.xxxxxxx.com

go build的时候系统就不会用GOPROXY以及不再校验SUM

3、调整git https===>ssh,注意username换成自己的用户名

[url "username@git.xxxxxxx.com"]
    insteadOf = https://git.xxxxxxx.com

结论

这个问题,google 堪称一绝,够任性

补充:go get拉取私有项目,遇到 404 Not Found解决办法

问题

利用go module进行包管理的时候,要获取远程仓库的最新包,使用go get+项目名获取,提示404 Not Found,如图:

在这里插入图片描述

原因及解决办法 原因

这是由于go get在进行获取远程包的时候,没有指定用户以及密码,导致没有权限,故失败

办法

go get时添加“-insecure”参数,如图:

在这里插入图片描述

补充:golang 配置私有仓库

配置使用ssh 访问的仓库

1. go mod 根据go.mod拉取依赖库时

会使用https的方式。为了方便我们也可以通过配置git 全局配置来使用 ssh的方式拉取依赖,下面是配置 https转换为ssh的方式:

git config --global url."git@gitee.com:".insteadOf https://gitee.com/

2. 配置环境变量

来指定私有仓库,用于不走代理的方式

go env -w GOPRIVATE=gitee.com

这里配置私有仓库是gitee

3. 设置代理

go env -w GOPROXY=goproyx.io

常见错误:

1. 错误一

abc@Genricde helloworld % go get -u gitee.com/abc/helloworld/v3 go: gitee.com/abc/helloworld/v3@v3.0.1-5 requires gitee.com/abc/tsab@v0.0.0-20210208082057-adbeb3ca5366: invalid version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in /Users/abc/developer/golang/pkg/mod/cache/vcs/742008abb4987f237c93efc5ddde7db6dd8d1841fe94aea076046d86a92e26a7: exit status 128: fatal: could not read Username for 'https://gitee.com': terminal prompts disabled

这种错误为没有配置 git 的https转换为 ssh

2. 错误二

go: gitee.com/abc/helloworld@v0.0.0-20210121064045-46ac6dd9cdce requires gitee.com/abc/helloworld@v0.0.0-20210208023819-88ebb8c504ae/go.mod: verifying module: gitee.com/abc/helloworld@v0.0.0-20210208023819-88ebb8c504ae/go.mod: reading https://goproxy.io/sumdb/sum.golang.org/lookup/gitee.com/abc/tsab@v0.0.0-20210208023819-88ebb8c504ae: 410 Gone server response: not found: gitee.com/abc/helloworld@v0.0.0-20210208023819-88ebb8c504ae: invalid version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in /tmp/gopath/pkg/mod/cache/vcs/742008abb4987f237c93efc5ddde7db6dd8d1841fe94aea076046d86a92e26a7: exit status 128: fatal: could not read Username for 'https://gitee.com': terminal prompts disabled

这种错误是GOPRIVATE 设置错误,使得go去验证库的sum

以上为个人经验,希望能给大家一个参考,也希望大家多多支持猪先飞。如有错误或未考虑完全的地方,望不吝赐教。

标签:[!--infotagslink--]

您可能感兴趣的文章: