华域联盟 Golang 完美解决golang go get私有仓库的问题

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

解决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 "[email protected]"]
    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."[email protected]:".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/[email protected] requires gitee.com/abc/[email protected]: 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/[email protected] requires gitee.com/abc/[email protected]/go.mod: verifying module: gitee.com/abc/[email protected]/go.mod: reading https://goproxy.io/sumdb/sum.golang.org/lookup/gitee.com/abc/[email protected]: 410 Gone server response: not found: gitee.com/abc/[email protected]: 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

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

本文由 华域联盟 原创撰写:华域联盟 » 完美解决golang go get私有仓库的问题

转载请保留出处和原文链接:https://www.cnhackhy.com/111305.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

Golang的func参数及返回值操作

golang elasticsearch Client的使用详解

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们