6

解决go get下载包失败问题

 3 years ago
source link: https://www.fdevops.com/2020/03/22/fix-go-get
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

解决go get下载包失败问题

兰玉磊 • 2020年3月22日 14:23 • Golang • 阅读 3187

由于某些不可抗力的原因,国内使用go get命令安装包时会经常会出现timeout的问题。本文介绍几个常用的解决办法。

从github克隆

golang在github上建立了一个镜像库,如https://github.com/golang/net就对应是 https://golang.org/x/net的镜像库。 要下载golang.org/x/net包,可以在本地创建包的目录后使用git clone来拉取相应包的源代码文件,具体操作如下:

mkdir -p $GOPATH/src/golang.org/x
cd $GOPATH/src/golang.org/x
git clone https://github.com/golang/net.git

使用gopm

使用gopm可以从golang.org的一些镜像网站上下载。

安装gopm

go get -u github.com/gpmgo/gopm

使用gopm安装第三方包

不加-g参数,会把依赖包下载.vendor目录下面; 加上-g参数,可以把依赖包下载到GOPATH目录中。

gopm get -g golang.org/x/net  

使用goproxy

Go的1.11版本以后 可以设置GOPROXY这个变量,来设置代理。 如果你自己有代理服务器就可以把这个环境变量设置成自己的代理。 首先开启go module支持:

在Mac/linux下执行:

export GO111MODULE=on

Windows平台执行:

SET GO111MODULE=on

2019.6.10更新:goproxy.cn

我们这里以https://goproxy.cn为例:

在Mac/linux下可以执行以下命令来设置:

export GOPROXY=https://goproxy.cn

Windows平台在cmd执行以下命令来设置:

SET GOPROXY="https://goproxy.cn"

或者在PowerShell中执行:

C:\> $env:GOPROXY = "https://goproxy.cn"

本文为转载文章,贵在分享,版权归原作者及原出处所有,如涉及版权等问题,请及时与我联系。
原文出处:李文周的博客
原文链接:https://www.liwenzhou.com/posts/Go/fix_go_get


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK