在 CentOS 上给 Golang 配置代理,通常有两种常见场景:HTTP/HTTPS 网络代理(访问外网) 和 Go 模块代理(GOPROXY)。下面分别说明。
go env -w GOPROXY=https://goproxy.cn,direct
或(官方全球代理):
go env -w GOPROXY=https://proxy.golang.org,direct
go env -w GONOPROXY=git.company.com
go env -w GONOSUMDB=git.company.com
go env -w GOPRIVATE=git.company.com
go env
如果你的服务器需要通过代理访问互联网:
export http_proxy=http://127.0.0.1:7890
export https_proxy=http://127.0.0.1:7890
export no_proxy=localhost,127.0.0.1
vi ~/.bashrc
添加:
export http_proxy=http://127.0.0.1:7890
export https_proxy=http://127.0.0.1:7890
然后:
source ~/.bashrc
Go 默认不读取 http_proxy,需要显式设置:
go env -w HTTP_PROXY=http://127.0.0.1:7890
go env -w HTTPS_PROXY=http://127.0.0.1:7890
go env -w GOPROXY=https://goproxy.cn,direct
go env -u HTTP_PROXY
go env -u HTTPS_PROXY
go env -u GOPROXY
go: cannot find main modulego mod init projectname
git config --global http.proxy http://proxy:port
systemctl stop firewalld
如果你能说明:
我可以给你更精确的配置方案。