206

golang包管理镜像仓库

 5 years ago
source link: http://ieevee.com/tech/2019/02/19/go-mod-proxy.html?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

golang写代码,有个地方特别不爽:没有一个成熟的包管理机制。前文 Golang官方包依赖管理工具 go mod 简明教程 里介绍了golang 1.11版本退出的go module功能,但是实际使用时还是会遇到网络的问题。

众所周知,golang的google推出的,而google又长期处于404状态;我们有时会遇到一些托管在 golang.org , k8s.gcr.io 的库,在go mod之前可以从github拖下来改下名字,但go mod之后这个方法就行不通了,影响go mod的使用。

事实上在golang 1.11版本推出go mod的同时,还推出了一个新的环境变量 GOPROXY ,它的作用类似http(s)_proxy,用于为golang代码仓库做镜像代理。

有了这个变量,我们只需要再找一个稳定可靠的镜像仓库就可以了。

jfrog提供的 GoCenter 就是这样一个镜像仓库。

使用方法非常简单,配置环境变量即可:

export GOPROXY=https://gocenter.io

kube-role-finder 为例,编译命令为

GOPROXY="https://gocenter.io" GO111MODULE=on go build

注意,GOPROXY开启以后,若失败不会自动回源。

GoCenter还推出了goc工具,它可以自动回源,如果你希望使用可以参考GoCenter的说明。

如果你有使用Athens私有仓库,可以将GOPROXY设置为Athens,然后将GoCenter设置为Athens的remote repository。

另外,按golang官方的 blog ,其2019年会推出一个官方的镜像服务,拭目以待吧~


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK