Problem description
When we use go get
, When executing commands such as go install
, go mod
, etc., the corresponding packages or dependent packages will be automatically downloaded. However, due to well-known reasons, packages similar to golang.org/x/...
will fail to download. As shown below:
$ go get -u golang.org/x/sys go get golang.org/x/sys: 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: i/o timeout)
Solution
So how do we solve the problem? After all, we still need to create bugs~
Manual download
Our common golang.org/x/...
packages are generally available on GitHub. Mirror warehouse correspondence. For example, golang.org/x/text
corresponds to github.com/golang/text
. Therefore, we can manually download or clone the corresponding GitHub repository to the specified directory.
mkdir $GOPATH/src/golang.org/x cd $GOPATH/src/golang.org/x git clone git@github.com:golang/text.git rm -rf text/.git
When you need to specify the version, this method has no solution, because most of the mirror warehouses on GitHub do not have tags. Moreover, how can programmers do it manually, especially because there are too many dependencies.
Set proxy
If you have a proxy, you can set the corresponding environment variable:
export http_proxy=http://proxyAddress:port export https_proxy=http://proxyAddress:port
Or, use all_proxy## directly #:
export all_proxy=http://proxyAddress:portgo mod replaceStarting from Go 1.11 version, new support for
go modules is used to solve package dependency management issues. This tool provides
replace, which is to solve the problem of package aliases and also solve the problem that
golang.org/x cannot be downloaded.
go module is integrated into the native
go mod command, but if your code base is in
$GOPATH,
The module function is not enabled by default. It is very simple to enable it. You can enable it through an environment variable
export GO111MODULE=on.
module example.com/hello require ( golang.org/x/text v0.3.0 ) replace ( golang.org/x/text => github.com/golang/text v0.3.0 )Similarly, there are third-party package management tools such as glide and gopm, which provide us with different solutions. GOPROXY environment variableFinally comes the ultimate killer of this article -
GOPROXY.
We know that starting from theGo 1.11 version, the
go module package dependency management tool is officially supported.
GOPROXY environment variable has also been added. If this variable is set, the source code will be downloaded through the proxy address set by this environment variable, instead of downloading directly from the code library as before. This is undoubtedly the greatest good news for us developing citizens who cannot access the Internet scientifically.
GOPROXY proxy service with one click. At the same time, a public proxy service
https://goproxy.io is also provided. We only need to set this environment variable to download the blocked source code package normally:
export GOPROXY=https://goproxy.ioHowever,
Needs to depend on go module function
. MODULE can be turned on via export GO111MODULE=on.
GOPATH, you cannot use
go get ..., but you can use
go mod ... related commands.
export GOPROXY=.
PowerShell:
$env:GOPROXY = "https://goproxy.io"Finally, we certainly recommend using the
GOPROXY solution to this environment variable, provided that Is
Go version >= 1.11.