others-How to solve go get unrecognized import path error?

How to solve unrecognized import path error when doing go get with golang ?


When you want to download a golang dependency from a golang repository or from github repository, you would get this error:

go get -u github.com/prometheus/client_golang/prometheus          
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/sys": https fetch: Get "https://golang.org/x/sys?go-get=1": dial tcp i/o timeout

The -u flag means:

The -u flag instructs get to update modules providing dependencies of packages named on the command line to use newer minor or patch releases when available. Continuing the previous example, ‘go get -u A’ will use the latest A with B v1.3.1 (not B v1.2.3). If B requires module C, but C does not provide any packages needed to build packages in A (not including tests), then C will not be updated.

Here we tried to download a prometheus library from golang, but we got unrecognized import path error.


You should specify the proxy before doing the go get job.

You should use this tag:

export GOPROXY=https://goproxy.io

What is a GOPROXY?

By default the go command downloads modules from VCS’s directly. The GOPROXY environment variable allows further control over the download source. The environment variable configures the go command to use a Go module proxy.

By setting the GOPROXY environment variable to a Go module proxy, you can overcome all of the disadvantages listed above:

  • The Go module proxy is by default caching and storing all the dependencies forever (in immutable storage). This means you don’t have to use any vendor/ folder anymore.

  • Getting rid of the vendor/ folder means your projects won’t take space in your repository. Because the dependencies are stored in immutable storage, even if a dependency disappears from the internet, you’re protected against it.
  • It’s not possible to override or delete a Go module once it’s stored in the Go proxy. This protects you against actors who might inject malicious code with the same version.
  • You don’t require any VSC tools anymore to download the dependencies because the dependencies are served over HTTP (Go proxy uses HTTP under the hood).
  • It’s significantly faster to download and build your Go module because Go proxy serves the source code (.zip archive) and go.mod independently over HTTP. This causes the downloads to take less time and faster (due to less overhead) compared to fetching from a VCS. Resolving dependencies is also faster because the go.mod can be fetched independently (whereas before it had to fetch the whole repository). The Go team tested it andthey saw a 3x speedup on fast networks and 6x on slow networks!
  • You can easily run your own Go proxy, which gives you more control over the stability of your build pipeline and protects against the rare cases when the VCS is down.

What is goproxy.io?

goproxy.io is A global proxy for Go modules, it is one of the world’s earliest Go modules mirror proxy services, developed and maintained by a group of young people who love open source and the Go language


You can setup your goproxy.io by using these commands:

For MAC/Linux Users:

# add environment to your profile
echo "export GOPROXY=https://goproxy.io" >> ~/.profile && source ~/.profile

# if your terminal is zsh,type the command below
echo "export GOPROXY=https://goproxy.io" >> ~/.zshrc && source ~/.zshrc

For Windows Users:

1. Right click This PC -> Properties -> Advanced system settings -> Environment Variables
2. Click "New" in Environment Variables
3. Input Variable Name: “GOPROXY”
4. Input Variable Value: “https://goproxy.io”
5. Click "OK", save your settings.

Run again

go get -u github.com/prometheus/client_golang/prometheus  

go: downloading github.com/prometheus/client_golang v1.7.1
go: found github.com/prometheus/client_golang/prometheus in github.com/prometheus/client_golang v1.7.1
go: downloading github.com/prometheus/procfs v0.1.3
go: downloading github.com/prometheus/common v0.10.0
go: downloading github.com/golang/protobuf v1.4.2
go: downloading github.com/beorn7/perks v1.0.1
go: downloading github.com/cespare/xxhash/v2 v2.1.1
go: downloading github.com/prometheus/client_model v0.2.0
go: downloading golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1
go: downloading google.golang.org/protobuf v1.23.0
go: downloading github.com/matttproud/golang_protobuf_extensions v1.0.1
go: github.com/golang/protobuf upgrade => v1.4.2
go: github.com/beorn7/perks upgrade => v1.0.1
go: golang.org/x/sys upgrade => v0.0.0-20200828194041-157a740278f4
go: github.com/prometheus/procfs upgrade => v0.1.3
go: github.com/prometheus/common upgrade => v0.13.0
go: github.com/prometheus/client_model upgrade => v0.2.0
go: google.golang.org/protobuf upgrade => v1.25.0
go: downloading golang.org/x/sys v0.0.0-20200828194041-157a740278f4
go: downloading google.golang.org/protobuf v1.25.0
go: downloading github.com/prometheus/common v0.13.0

We complete the job successfully.