go编译中下载依赖包出现的网络错误

在用golang进行build的时候,会发现类似这样的错误:

Get “https://proxy.golang.org/cloud.google.com/go/storage/@v/v1.21.0.zip”: dial tcp 142.251.42.241:443: connectex: A connection attempt failed be
cause the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

在这里插入图片描述

在这个时候,可以考虑先运行以下两条命令:

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

第一条命令是设置代理,第二条命令涉及到Go.mod,这个是Golang1.11版本新引入的官方包管理工具,用于解决之前没有地方记录依赖包具体版本的问题,方便依赖包的管理。

运行go mod tidy,可以引用项目需要的依赖增加到go.mod文件,以及去掉go.mod文件中项目不需要的依赖。一般运行这行之后,会开始自动下载一些包:

在这里插入图片描述
然后再去运行go build或者make,就可以成功了。

猜你喜欢

转载自blog.csdn.net/weixin_43997331/article/details/125736246