注:Golang 从
1.13开始默认使用Go Mod,请切换至Go Mod并配置goproxy
问题描述
国内使用go get命令时,Google 相关的域名例如golang.org经常被墙。
方法 1:使用 Gopm
注:Gopm 目前已停止维护
使用gopm get命令替代go get:
> go get -u github.com/gpmgo/gopm
> gopm
NAME:
Gopm - Go Package Manager
USAGE:
Gopm [global options] command [command options] [arguments...]
VERSION:
0.8.8.0307 Beta
COMMANDS:
list list all dependencies of current project
gen generate a gopmfile for current Go project
get fetch remote package(s) and dependencies
bin download and link dependencies and build binary
config configure gopm settings
run link dependencies and go run
test link dependencies and go test
build link dependencies and go build
install link dependencies and go install
clean clean all temporary files
update check and update gopm resources including itself
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--noterm, -n disable color output
--strict, -s strict mode
--debug, -d debug mode
--help, -h show help
--version, -v print the version
方法 2:install from Github
方法 3:配置终端代理
阅读get.go源码会发现,go get命令通过git clone命令将远程仓库的代码拉取到本地。
根据官方 golang/go - GoGetProxyConfig | Github 的说明,需要设置git的代理:
> git config [--global] http.proxy http://proxy.example.com:port
然而并没有起作用。。
我在
Linux和Windows的机器上都开启了Shadowsocks代理,本地端口为1080
搜索了一圈之后发现,需要设置http_proxy和https_proxy这两个环境变量。
for Linux
我在CentOS 7.5的机器上已经使用ProxyChains-NG作为终端命令的代理程序,这样可以很方便的在需要代理的时候在命令前加上pc前缀(之前设置了alias pc='proxychains4')。
而添加http_proxy环境变量后,所有终端命令的 HTTP 连接都会走代理,这并非我所期望的。因此不能直接在~/.zshrc中添加环境变量。
我的解决方案是:将设置http_proxy与https_proxy环境变量的export命令单独写在 Shell 脚本中,需要走代理时直接source即可。
首先以下内容另存为export-http-proxy.sh:
#!/bin/bash
export http_proxy=socks5://127.0.0.1:1080 # 代理地址
export https_proxy=$http_proxy
export | grep http
之后添加执行权限
> chmod +x export-http-proxy.sh
最后
> source export-http-proxy.sh
http_proxy=socks5://127.0.0.1:1080
https_proxy=socks5://127.0.0.1:1080
这样就可以直接go get被墙的包了。
for Windows
Windows 就非常简单了,直接设置以下环境变量:
http_proxy socks5://127.0.0.1:1080 # 代理地址
https_proxy socks5://127.0.0.1:1080
要想临时添加代理,只需将以下内容保存为http-proxy.bat,需要时执行即可:
set http_proxy=socks5://127.0.0.1:1080
set https_proxy=%http_proxy%