3

如何在Linux配置Go语言Package包中国镜像

 3 years ago
source link: https://www.myfreax.com/how-to-configure-the-go-language-package-mirroring-in-linux/
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.
neoserver,ios ssh client

这个世界有的,在朝里永远都有另一份的独特的存在。好像我们不是在地球村的一样。如果你一直卡在go get golang.org/x/XX ,那么就对,你是在中国。

言归正传,在本教程中我们将讨论如何配置Golang语言的中国镜像,让获取Go语言包变得更快。

如果你使用的是一个私有包,你需要使用代理来解决这种问题。下面提供几种方案供你选择。

私有包是不能使用公共镜像,请自行配置代理。

由于我们网络环境特殊性,在构建环境之前先准备好科学上网工具shadowsocks和http代理工具polipo或者是Wireguard。(仅供学习)

使用github上的镜像

获取Golang Package在github镜像上的路径: golang.org/x/PATH_TO_PACKAGE --> github.com/golang/PATH_TO_PACKAGE.

   // Ex:
   golang.org/x/net/context --> github.com/golang/net/context

运行go get来安装github镜像的Golang packages。

   // Ex:
   go get github.com/golang/net/context

你会碰到如下错误提示:

   package github.com/golang/net/context:
   code in directory /go/src/github.com/golang/net/context
   expects import "golang.org/x/net/context"

配置中国Golang语言的包镜像

export GOPROXY=https://goproxy.io,direct

现在,当你构建你Golnag应用时,Go将会获取通过 goproxy.io获取包,如果你想持久化环境变量,请执行以下命令,如果你使用的Golang版本< 1.13,请升级Go到最新版本,请阅读文档如何安装Go最新版本

echo export GOPROXY=https://goproxy.io,direct >> ~/.zshrc
zsh
echo export GOPROXY=https://goproxy.io,direct >> ~/.zshrc
bash
how-to-set-up-wireguard-vpn-on-ubuntu-20-04.jpg
how-to-install-go-on-ubuntu-20-04.jpg

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK