好久不更新博客了,都长草了;被迫营业从头学起Go语言了,特开新文章分类记录go学习成长的点滴,内容可能就没有那么专注了,遇到问题解决了就写啥。

Go mod墙

很不巧,golang官网被我大天朝拦在墙外,还好有一个代替地址:https://golang.google.cn/

开源不等于免费

配置:

go env -w GOPROXY="https://goproxy.io,direct"

go env -w GOSUMDB="gosum.io+ce6e7565+AY5qEHUk/qmHc5btzW45JVoENfazw8LielDsaI+lEbq6"
-w

go mod 私有包

上述go mod代理解决了公开mod包的引用问题,但是企业项目是有私密的,这涉及到私有包的导入问题。

按go的版本区分并做了细致的说明,我所安装的版本是1.14,解决思路如下:

GOPRIVATE

2、go get命令是走的https协议,对于私有git类型的远程仓库需要强制转换为ssh协议

https://github.com/private_org/private_repo
github.com/private_org/private_repo
GOPRIVATE
go env -w GOPRIVATE="github.com/private_org/private_repo"
GOPRIVATEgithub.com/private_org/private_repo_01
go env -w GOPRIVATE="github.com/private_org/private_repo,github.com/private_org/private_repo_01"

因为支持前缀通配匹配,可以如下改写,这样就意味着github.com/private_org下所有包都是私有包

go env -w GOPRIVATE="github.com/private_org"

2、设置git强制走ssh协议get私有包代码

因为go get默认走https协议,私有包肯定都不能直接通过一个url来下载代码的,而是需要先登录,而ssh刚好可以透明的处理鉴权这一步,而刚好git又支持这么一个功能:即强制将http或https协议的远程仓库抓取透明的变更为ssh协议。

~/.gitconfig
[url "git@github.com:"]
	insteadOf = https://github.com

当然你也可以命令行直接操作:

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

当然如果你的地址不是github的,其原理是一样的,更换对应你自己的git远程仓库域名即可。

git