在 CentOS 上配置 Golang 的代理(通常指 Go 模块代理 GOPROXY),推荐按照下面几种常见场景来配置,既稳定又快。
如果你在国内,强烈建议使用以下代理:
go env -w GOPROXY=https://proxy.golang.com.cn,direct
go env -w GOSUMDB=sum.golang.google.cn
或使用官方代理 + 国内镜像:
go env -w GOPROXY=https://goproxy.cn,https://proxy.golang.org,direct
✅ goproxy.cn 是七牛维护的,速度非常快
✅ direct 表示如果代理失败,直接连源
只在当前终端生效:
export GOPROXY=https://goproxy.cn,direct
export GOSUMDB=sum.golang.google.cn
编辑用户环境变量:
vi ~/.bashrc
添加:
export GOPROXY=https://goproxy.cn,direct
export GOSUMDB=sum.golang.google.cn
export GO111MODULE=on
然后:
source ~/.bashrc
如果有私有 Git 仓库(如 GitLab),需要排除代理:
go env -w GOPRIVATE=git.company.com,github.com/org/private
这样 Go 不会通过代理拉取私有代码。
go env
重点看这几项:
GOPROXYGOSUMDBGOPRIVATEGO111MODULE❌ 不要只写 https://proxy.golang.org(国内很慢)
❌ 不要关闭 GOSUMDB(除非你明确知道风险)
✅ CentOS 7/8 都适用
✅ Go 1.13+ 才支持 go env -w
如果你是在 Docker / CI / 服务器无法联网 场景下用,我也可以给你具体方案。