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

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

报错信息:

go: golang.org/x/[email protected]: 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/[email protected]: 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/[email protected]: 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

你可能感兴趣的:(编程语言,go)