49

【golang包管理】 GPROXY使用

 4 years ago
source link: https://www.tuicool.com/articles/2qYNJnu
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

&痛点:

使用 go get 命令 自动下载相应的包时,由于国内网络原因会出现各种 类似于 golang.org/x/... 的问题

&解决方案

使用 golang 的 go module 包管理工具(golang版本 >= 1.11),设置 goproxy 即可

&步骤

虽然下面的内容主要是讲解如何设置 GOPROXY ,但是我们也推荐你在使用 Go 模块时将 GO111MODULE 设置为 on 而不是 auto

Go 1.13 及以上(推荐)

打开你的终端并执行:

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

完成。

macOS 或 Linux

打开你的终端并执行:

$ export GOPROXY=https://goproxy.cn

或者

$ echo "export GOPROXY=https://goproxy.cn" >> ~/.profile && source ~/.profile

完成。

Windows

打开你的 PowerShell 并执行:

C:\> $env:GOPROXY = "https://goproxy.cn"
1. 打开“开始”并搜索“env”
2. 选择“编辑系统环境变量”
3. 点击“环境变量…”按钮
4. 在“<你的用户名> 的用户变量”章节下(上半部分)
5. 点击“新建…”按钮
6. 选择“变量名”输入框并输入“GOPROXY”
7. 选择“变量值”输入框并输入“https://goproxy.cn”
8. 点击“确定”按钮

完成。

PS:

  1. 使用Goland时创建module项目时要设置Proxy

    yyqmMvr.png!web

    创建项目时要设置Proxy

    一劳永逸更改

    V3yyEbb.png!web

    image.png

  2. 如果你使用的 Go 版本>=1.13, 你可以通过设置 GOPRIVATE 环境变量来控制哪些私有仓库和依赖(公司内部仓库)不通过 proxy 来拉取,直接走本地,设置如下:

# go 版本 >= 1.13
go env -w GOPROXY=https://goproxy.io,direct
# 设置不走 proxy 的私有仓库,多个用逗号相隔
go env -w GOPRIVATE=*.corp.example.com

参考资料:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK