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:
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:
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.
You can set up goproxy.io using the following commands:
For macOS/Linux Users:
For Windows Users:
- Right-click This PC -> Properties -> Advanced system settings -> Environment Variables.
- Click New in Environment Variables.
- Input Variable Name:
GOPROXY
. - Input Variable Value:
https://goproxy.io
. - Click OK to save your settings.
Run Again
After setting up the proxy, run the following command:
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:
- 👨💻 Go Modules Proxy
- 👨💻 goproxy.io
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!