在 Debian 上配置 Golang 代理(下载依赖的代理),通常是指配置 GOPROXY,用于加速 go mod 拉取依赖。
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 -w GOPROXY=https://goproxy.cn,direct
go env -w GOSUMDB=sum.golang.org
查看是否生效:
go env GOPROXY
go env -w GOPRIVATE=git.example.com,github.com/org/private
这样不会走代理。
go env -u GOPROXY
go mod download
或新建项目:
go mod init test
go get github.com/gin-gonic/gin
go env -w GOPROXY=https://goproxy.cn,direct
go env -w GOSUMDB=sum.golang.org
如果你是指 HTTP 网络代理(如公司翻墙),而不是 GOPROXY,也可以告诉我,我可以单独讲 http_proxy 的配置。