温馨提示×

debian golang代理如何配置

小樊
76
2026-10-11 03:45:39
栏目: 编程语言

在 Debian 上配置 Golang 代理(主要是模块代理 GOPROXY),通常有以下几种方式,适用于不同场景。


一、配置 Go 模块代理(最常用)

Go 从 1.13 开始使用 GOPROXY 下载依赖。

1. 临时设置(当前终端有效)

export GOPROXY=https://proxy.golang.org,direct

2. 国内常用代理(推荐)

export GOPROXY=https://goproxy.cn,direct

或

export GOPROXY=https://goproxy.io,direct

3. 永久生效(推荐)

写入 ~/.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

三、配置 Go 私有仓库(不走代理)

如果有私有仓库(如 GitHub Enterprise、GitLab):

go env -w GOPRIVATE=git.example.com,github.com/org/private

四、通过系统 HTTP 代理访问网络(可选)

如果你的服务器只能走 HTTP 代理上网:

export HTTP_PROXY=http://127.0.0.1:7890
export HTTPS_PROXY=http://127.0.0.1:7890

Go 会遵循该代理(用于 git 拉取等情况)。


五、Debian 上安装 Go(补充)

如果还没装 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

如果你是想问:

  • ✅ Go 模块代理
  • ✅ 系统网络代理
  • ✅ Docker 内 Go 代理
  • ✅ CI/CD 中配置

可以告诉我具体场景,我可以给你更精确的配置。

0 踩