Skip to content

How to resolve go get unrecognized import path error?

How to resolve unrecognized import path error when doing go get with Golang?

Problem

When you want to download a Golang dependency from a repository or GitHub, you might encounter this error:

Terminal window
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 216.239.37.1:443: 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 got the unrecognized import path error.

Solution

To resolve this issue, you should specify a proxy before running the go get command. Use the following command:

Terminal window
export GOPROXY=https://goproxy.io

What is a GOPROXY?

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

By setting the GOPROXY environment variable to a Go module proxy, you can overcome several disadvantages:

  • The Go module proxy caches and stores all dependencies forever (in immutable storage), eliminating the need for a vendor/ folder.
  • Removing the vendor/ folder reduces repository size.
  • Dependencies stored in immutable storage are protected from disappearing from the internet.
  • Go modules cannot be overridden or deleted once stored in the proxy, protecting against malicious code injection.
  • No VCS tools are required for dependency downloads, as dependencies are served over HTTP.
  • Dependency resolution and downloads are significantly faster due to reduced overhead.

What is goproxy.io?

goproxy.io is a global proxy for Go modules. It is one of the world’s earliest Go module mirror proxy services, developed and maintained by a group of open-source enthusiasts and Go language lovers.

image-20200831174207542

You can set up goproxy.io using the following commands:

For macOS/Linux Users:

~/.profile
# Add environment to your profile
echo "export GOPROXY=https://goproxy.io" >> ~/.profile && source ~/.profile
# If your terminal is zsh, use 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 to save your settings.

Run Again

After setting up the proxy, run the following command:

Terminal window
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

The job is now completed successfully.

Summary

This post explained how to resolve the “unrecognized import path” error when using go get in Golang. By setting up a Go module proxy like goproxy.io, you can ensure faster and more reliable dependency management. The proxy caches dependencies, protects against dependency loss, and eliminates the need for a vendor/ folder. Whether you’re using macOS, Linux, or Windows, setting up goproxy.io is straightforward and highly beneficial for your Go projects.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!