go.mod方式下gopm get安装成功,import之后还是报错的解决办法

更新

下载之前更新一下源就没那么多事了

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

————————————————————————————————————————————

版本

go 1.13

过程

一开始直接go go get gopkg.in/gomail.v2时提示:

go get gopkg.in/gomail.v2: module gopkg.in/gomail.v2: Get 
https://proxy.golang.org/gopkg.in/gomail.v2/@v/list: dial tcp 216.58.200.49: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.

后面直接用gopm get -g gopkg.in/gomail.v2,安装成功了,自动安装到/$GOPATH/src/gopkg.in下,因为项目用的是go.mod的方式,就把包复制到了 $GOPATH/pkg/mod/gopkg.in下,然后再项目里 直接 import "gopkg.in/gomail.v2"
结果也找到包了,但是还是下载失败

go: finding gopkg.in/gomail.v2 latest
go: downloading gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
verifying gopkg.in/gomail.v2@v2.0.0-20160411212932-81ebce5c23df: 
gopkg.in/gomail.v2@v2.0.0-20160411212932-81ebce5c23df: Get 
https://sum.golang.org/lookup/gopkg.in/gomail.v2@v2.0.0-20160411212932-
81ebce5c23df: dial tcp 172.217.24.17: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 1.13之后默认会用sum校验,就是go.sum,由于刚刚下载的包虽然下载成功了,但是目录名是gomail.v2,所以module还是认为在你这里找不到包,于是会自动去下载,后面的就清除了,因为大陆访问不了gopkg.in,所以下载失败。。

现在只需要将 GOPROXY设置一下就好了

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

然后运行项目

go run ./main.go
go: finding gopkg.in/gomail.v2 latest                      
go: downloading gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
go: extracting gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df

成功,然后可以看到go.sum添加了如下信息

gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=

总结

有时候gopm get下载下来之后,发生上面的情况,可以按照报错的提示,将目录名修改一下就可以了,例如 提示 verifying gopkg.in/[email protected]:,直接把刚刚gopm安装的包名改一下[email protected]:就可以了

你可能感兴趣的:(Go)