Go更换国内源配置环境变量

背景

        要在中国境内下载和使用Go编程语言的包,可以使用国内的Go模块代理来加速下载速度。以下是一些常见的国内Go模块代理源以及如何切换到这些源的方法:

常见国内Go模块代理源

    七牛云(Qiniu) https://goproxy.cn

    阿里云(Alibaba Cloud) https://mirrors.aliyun.com/goproxy/

    清华大学(Tsinghua University) https://goproxy.tuna.tsinghua.edu.cn

切换Go模块代理源的方法

    通过环境变量配置
            在终端中执行以下命令:

go env -w GOPROXY=https://goproxy.cn,direct

    通过配置文件配置
            你也可以在Go的配置文件中手动修改GOPROXY的值。找到并编辑go的配置文件(通常位于$HOME/.config/go/env),添加或修改以下行:

GOPROXY=https://goproxy.cn,direct

以下是使用七牛云源的示例:

go env -w GOPROXY=https://goproxy.cn,direct

类似的,你可以根据需要选择其他代理源,例如:

go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct

go env -w GOPROXY=https://goproxy.tuna.tsinghua.edu.cn,direct

另外通过环境变量永久配置

linux/mac

# 设置你的 bash 环境变量
echo "export GOPROXY=https://goproxy.io,direct" >> ~/.profile && source ~/.profile

# 如果你的终端是 zsh,使用以下命令
echo "export GOPROXY=https://goproxy.io,direct" >> ~/.zshrc && source ~/.zshrc

官方说明
# Set the GOPROXY environment variable
export GOPROXY=https://goproxy.io,direct
# Set environment variable allow bypassing the proxy for specified repos (optional)
export GOPRIVATE=git.mycompany.com,github.com/my/private

windows

1. 右键 我的电脑 -> 属性 -> 高级系统设置 -> 环境变量
2. 在 “[你的用户名]的用户变量” 中点击 ”新建“ 按钮
3. 在 “变量名” 输入框并新增 “GOPROXY”
4. 在对应的 “变量值” 输入框中新增 “https://goproxy.io,direct”
5. 最后点击 “确定” 按钮保存设置

官方说明
# Set the GOPROXY environment variable
$env:GOPROXY = "https://goproxy.io,direct"
# Set environment variable allow bypassing the proxy for specified repos (optional)
$env:GOPRIVATE = "git.mycompany.com,github.com/my/private"

配置完成后,你就可以快速下载和使用Go的包了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

架构师之路魂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值