解决go get golang.org/x 包失败

某天在用golang写代码,需要用到ssh功能,于是在网上搜了一下发现有个golang.org/x/crypto/ssh包,很自然就想到要安装

go get -u golang.org/x/crypto/ssh

你会发现由于golang.org官网各种网络问题,很难安装

哈哈,找到一种goproxy的解决方案,该方案适用于go 1.13及以上的版本

ding@ding-Lenovo:~$ go version
go version go1.13.1 linux/amd64
ding@ding-Lenovo:~$

打开终端,用下面命令直接设置

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

设置完之后,我以为直接生效了,又去go get了一下,发现还是报一样的错,有点失望,原来需要依赖于 go module 功能,即go的环境变量GO111MODULE要打开,如何打开?如下命令即可,在终端执行

go env -w GO111MODULE=on

打开GO111MODULE设置前后对比

打开前

ding@ding-Lenovo:~$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN="/home/ding/tools/go/go1.13.1/bin"
GOCACHE="/home/ding/.cache/go-build"
GOENV="/home/ding/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/ding/tools/go/gopath"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/home/ding/tools/go/go1.13.1"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/ding/tools/go/go1.13.1/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build606174629=/tmp/go-build -gno-record-gcc-switches"

打开后

ding@ding-Lenovo:~$ go env 
GO111MODULE="on"
GOARCH="amd64"
GOBIN="/home/ding/tools/go/go1.13.1/bin"
GOCACHE="/home/ding/.cache/go-build"
GOENV="/home/ding/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/ding/tools/go/gopath"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/home/ding/tools/go/go1.13.1"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/ding/tools/go/go1.13.1/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build480318168=/tmp/go-build -gno-record-gcc-switches"

只要注意GO111MODULE项

此番设置后,再来安装ssh模块,看结果如何?

ding@ding-Lenovo:~$ go get -u golang.org/x/crypto/ssh
go: finding golang.org/x/crypto latest
go: downloading golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
go: extracting golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
go: downloading golang.org/x/sys v0.0.0-20190412213103-97732733099d
go: extracting golang.org/x/sys v0.0.0-20190412213103-97732733099d
go: finding golang.org/x/sys latest
go: downloading golang.org/x/sys v0.0.0-20191010194322-b09406accb47
go: extracting golang.org/x/sys v0.0.0-20191010194322-b09406accb47

哈哈,真爽!成功安装!!!感谢七牛云对go的支持, https://goproxy.cn 就是他们家的无私贡献。

愉快的go代码去。。。

我来评几句
登录后评论

已发表评论数()

相关站点

热门文章