Ubuntu配置Golang代理的步骤
Golang通过GOPROXY
环境变量管理模块代理,这是配置代理的关键步骤。
your.proxy.server
替换为实际代理地址,如goproxy.cn
):export GOPROXY="https://your.proxy.server,direct"
direct
表示若代理不可用,直接连接源服务器。~/.bashrc
或~/.profile
),然后运行source ~/.bashrc
(或source ~/.profile
)使更改生效。go env
命令永久写入配置(无需修改文件):go env -w GOPROXY=https://your.proxy.server,direct
若需为其他工具(如curl
)或手动下载依赖设置代理,可配置系统HTTP/HTTPS代理:
export http_proxy="http://your.proxy.server:port"
export https_proxy="http://your.proxy.server:port"
~/.bashrc
或~/.profile
,并执行source
命令生效。若通过Git克隆模块(如go get
),需额外配置Git代理:
git config --global http.proxy http://your.proxy.server:port
git config --global https.proxy http://your.proxy.server:port
git@github.com
形式的SSH地址,需修改~/.ssh/config
文件,添加:Host github.com
ProxyCommand nc -x your.proxy.server:port %h %p
(需安装netcat
工具,且-x
参数指定代理地址)go env
,查看输出中的GOPROXY
是否为你设置的值。go get -v golang.org/x/net
(或其他模块),若能正常下载且无网络错误,则代理生效。goproxy.cn
、goproxy.io
),提升访问速度。http://username:password@your.proxy.server:port
)。GO111MODULE=on
),无需额外设置。