Quickly solve the problem of unsuccessful go get golang.org/x package

藏色散人
Release: 2021-12-02 14:42:45
forward
3058 people have browsed it

This article is provided by the go language tutorial column to introduce how to solve the go get golang.org/x package failure problem with one click. I hope it will be helpful to friends in need!

One-click solution to go get golang.org/x package failure

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)
Copy after login

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
Copy after login

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
Copy after login

Or, use all_proxy## directly #:

export all_proxy=http://proxyAddress:port
Copy after login
go mod replace

Starting 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.

The following is a reference example:

module example.com/hello

require (
    golang.org/x/text v0.3.0
)

replace (
    golang.org/x/text => github.com/golang/text v0.3.0
)
Copy after login
Similarly, there are third-party package management tools such as glide and gopm, which provide us with different solutions.

GOPROXY environment variable

Finally comes the ultimate killer of this article -

GOPROXY.

We know that starting from the

Go 1.11 version, the go module package dependency management tool is officially supported.

In fact, the

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.

What’s even more gratifying is that the open source project goproxy.io helps us achieve what we want. This project allows developers to build their own

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.io
Copy after login
However,

Needs to depend on go module function. MODULE can be turned on via export GO111MODULE=on.

If the project is not in

GOPATH, you cannot use go get ..., but you can use go mod ... related commands.

It can also be turned off by setting this environment variable blank,

export GOPROXY=.

For Windows users, you can set it in

PowerShell:

$env:GOPROXY = "https://goproxy.io"
Copy after login
Finally, we certainly recommend using the

GOPROXY solution to this environment variable, provided that Is Go version >= 1.11.

Finally, Qiniu has also launched a domestic proxy goproxy.cn to facilitate domestic users to access inaccessible packages faster, which is really conscience.

The above is the detailed content of Quickly solve the problem of unsuccessful go get golang.org/x package. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template