温馨提示×

如何在CentOS上配置Golang代理

小樊
82
2025-03-11 11:49:05
栏目: 编程语言

在CentOS上配置Golang代理,可以通过设置环境变量来实现。以下是详细的步骤:

1. 设置HTTP代理

如果你需要通过HTTP代理访问互联网,可以设置HTTP_PROXYHTTPS_PROXY环境变量。

临时设置(仅当前终端会话有效)

export HTTP_PROXY=http://your.proxy.server:port
export HTTPS_PROXY=https://your.proxy.server:port

永久设置(对所有终端会话有效)

编辑你的shell配置文件(例如.bashrc.bash_profile),添加以下行:

export HTTP_PROXY=http://your.proxy.server:port
export HTTPS_PROXY=https://your.proxy.server:port

然后重新加载配置文件:

source ~/.bashrc  # 或者 source ~/.bash_profile

2. 设置GOPROXY环境变量

从Go 1.13开始,引入了GOPROXY环境变量,用于设置Go模块代理服务器。

临时设置(仅当前终端会话有效)

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

永久设置(对所有终端会话有效)

编辑你的shell配置文件(例如.bashrc.bash_profile),添加以下行:

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

然后重新加载配置文件:

source ~/.bashrc  # 或者 source ~/.bash_profile

3. 验证配置

你可以通过以下命令验证代理是否配置成功:

验证HTTP代理

curl -x http://your.proxy.server:port http://www.example.com

验证HTTPS代理

curl -x https://your.proxy.server:port https://www.example.com

验证GOPROXY

go env GOPROXY

4. 其他注意事项

  • 如果你的代理服务器需要认证,可以在URL中包含用户名和密码,例如:
    export HTTP_PROXY=http://username:password@your.proxy.server:port
    export HTTPS_PROXY=https://username:password@your.proxy.server:port
    
  • 确保你的代理服务器地址和端口是正确的。
  • 如果你在公司网络中,可能需要联系网络管理员获取正确的代理服务器地址和端口。

通过以上步骤,你应该能够在CentOS上成功配置Golang代理。

0