go moudle模块加载被墙解决方法

go moudle 为golang的一种包管理方式;而对于在国内使用,很多外网的包可能会无法使用,遂做一个记录;

报错信息:

go: golang.org/x/crypto@v0.0.0-20190701094942-4def268fd1a4: unrecognized import path "golang.org/x/crypto" (https fetch: Get https://golang.org/x/crypto?go-get=1: dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)
go: golang.org/x/sys@v0.0.0-20190813064441-fde4db37ae7a: unrecognized import path "golang.org/x/sys" (https fetch: Get https://golang.org/x/sys?go-get=1: dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)
go: golang.org/x/sys@v0.0.0-20190222072716-a9d3bda3a223: unrecognized import path "golang.org/x/sys" (https fetch: Get https://golang.org/x/sys?go-get=1: dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)

对于golang.org/x/**下的包,由于墙的原因,无法更新;解决方案如下:
方法1:
根据需要的版本号,如果不清楚,可以使用 v0.0.0 ,执行命令, 可以用github上的镜像地址替换:

go mod edit -require=golang.org/x/***@v0.0.0
go mod edit -replace=golang.org/x/***@v0.0.0=github.com/golang/***@latest

方法1设置后,go.mod文件中显示如下:

go 1.12
require golang.org/x/*** v0.0.0
replace golang.org/x/*** v0.0.0 => github.com/golang/*** latest

方法2:
配置代理地址:https://goproxy.io,在idea中可以直接配置go moudle的代理地址为这个url;
如果你使用的 Go 版本>=1.13, 你可以通过设置 GOPRIVATE 环境变量来控制哪些私有仓库和依赖(公司内部仓库)不通过 proxy 来拉取,直接走本地,设置如下:

go env -w GOPROXY=https://goproxy.io,direct
# 国内更快
go env -w GOPROXY=https://goproxy.cn,direct
# 设置不走 proxy 的私有仓库,多个用逗号相隔
go env -w GOPRIVATE=*.corp.example.com
  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值