其他分享
首页 > 其他分享> > go moudle模块加载被墙解决方法

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
# 设置不走 proxy 的私有仓库,多个用逗号相隔
go env -w GOPRIVATE=*.corp.example.com

标签:moudle,golang,failed,https,go,org,v0.0,加载
来源: https://blog.csdn.net/qq_35566365/article/details/100598621