在 Debian 上设置 Go(Golang)的代理,通常是为了加速模块下载(尤其是访问 proxy.golang.org 不稳定时)。最常见的是配置 Go Module Proxy,也可以使用 系统级 HTTP 代理。
export GOPROXY=https://proxy.golang.org,direct
国内常用镜像:
export GOPROXY=https://goproxy.cn,direct
# 或
export GOPROXY=https://goproxy.io,direct
编辑 ~/.bashrc 或 ~/.profile:
nano ~/.bashrc
添加:
export GOPROXY=https://goproxy.cn,direct
export GOSUMDB=sum.golang.org
保存后执行:
source ~/.bashrc
sudo nano /etc/profile.d/go.sh
写入:
export GOPROXY=https://goproxy.cn,direct
然后:
source /etc/profile
go env 设置(推荐方式)go env -w GOPROXY=https://goproxy.cn,direct
go env -w GOSUMDB=sum.golang.org
查看当前配置:
go env GOPROXY
如果你的网络需要通过代理服务器访问外网:
export HTTP_PROXY=http://127.0.0.1:7890
export HTTPS_PROXY=http://127.0.0.1:7890
sudo nano /etc/environment
添加:
HTTP_PROXY="http://127.0.0.1:7890"
HTTPS_PROXY="http://127.0.0.1:7890"
go env -u GOPROXY
unset HTTP_PROXY HTTPS_PROXY
go env GOPROXY
go mod download
go env -w GOPROXY=https://goproxy.cn,direct
如果你是用 apt 安装 Go 还是 官方二进制安装,或者是否使用 corporate proxy / 校园网,可以告诉我,我可以给你更精确的建议。