在 Debian 上配置 Golang 代理(主要是模块代理 GOPROXY),通常有以下几种方式,适用于不同场景。
Go 从 1.13 开始使用 GOPROXY 下载依赖。
export GOPROXY=https://proxy.golang.org,direct
export GOPROXY=https://goproxy.cn,direct
或
export GOPROXY=https://goproxy.io,direct
写入 ~/.bashrc 或 ~/.profile:
echo 'export GOPROXY=https://goproxy.cn,direct' >> ~/.bashrc
source ~/.bashrc
如果是 zsh:
echo 'export GOPROXY=https://goproxy.cn,direct' >> ~/.zshrc
source ~/.zshrc
go env -w GOPROXY=https://proxy.golang.org,direct
查看当前配置:
go env GOPROXY
如果有私有仓库(如 GitHub Enterprise、GitLab):
go env -w GOPRIVATE=git.example.com,github.com/org/private
如果你的服务器只能走 HTTP 代理上网:
export HTTP_PROXY=http://127.0.0.1:7890
export HTTPS_PROXY=http://127.0.0.1:7890
Go 会遵循该代理(用于 git 拉取等情况)。
如果还没装 Go:
sudo apt update
sudo apt install golang-go
或官方二进制:
wget https://go.dev/dl/go1.22.5.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.22.5.linux-amd64.tar.gz
go env -w GOPROXY=https://goproxy.cn,direct
go env -w GOSUMDB=sum.golang.org
go env -w GOPRIVATE=git.company.com
如果你是想问:
可以告诉我具体场景,我可以给你更精确的配置。