How To Fix Go Mod Unknown Revision
After the introduction of Go mod you may have worked with Go modules and private repositories. When working on a Go project that uses the new Go modules package management and versioning system and working exclusively with public packages you won’t generally encounter any major issue.
However when trying to import a Go package that is into a private repository you might encounter problems when running Go build or Go modules commands. You might see an issue as follows:
$ go build go: github.com/acme-corporation/[email protected]: unknown revision v0.1.5
If you have had a similar error you are not alone. This means that Go modules can’t rightfully access a private package.
How to resolve Go mod unknown revision when accessing private repositories
1. Make sure you have set GO111MODULES
Make sure you are using Go modules correctly this tells Go to use Go modules in case you are running an old version or Go or you have disabled Go modules by mistake. This is necessary for running the next steps.
go env -w GO111MODULE=on
2. Add your organisation private repository to GOPRIVATE
The Go team has rightfully thought about the possiblity of having private packages when working with Go mod and created a help tool to describe such scenario
go help module-private The go command defaults to downloading modules from the public Go module mirror at proxy.golang.org. It also defaults to validating downloaded modules,regardless of source, against the public Go checksum database at sum.golang.org. These defaults work well for publicly available source code. The GOPRIVATE environment variable controls which modules the go command considers to be private (not available publicly) and should therefore not use the proxy or checksum database. The variable is a comma-separated list of glob patterns (in the syntax of Go's path.Match) of module path prefixes. For example, GOPRIVATE=*.corp.example.com,rsc.io/private
The next step should therefore be as follows, which sets our private repository as “private” using the GOPRIVATE env variable
go env -w GOPRIVATE=github.com/acme-corporation/internal-rpc-client
3. Make sure your git configuration is appropriate for private repositories
Go mod uses git under the hood to retrieve remote package information. You should therefore tell git how to access private packages by updating the git configuration
git config --global url."ssh://[email protected]:acme-corporation".insteadOf "https://github.com/acme-corporation"
This will allow your git client and consequently Go mod to use your ssh key to access and authenticate with github and access the private repository
4. Make sure to create a personal access token if using 2FA
In github specifically if you are using 2FA for your organisation, using your ssh key won’t work in this case and you will have instead to create a personal access tokenand use that to access your private repository
git config --global url."https://:[email protected]:acme-corporation".insteadOf "https://github.com/acme-corporation"
For more info about private modules feel free to checkout the official documentation https://golang.org/ref/mod#private-modules
Go modules YouTube Tutorial