go get: Add a module from a private GitHub repository

GitHub private repository go get

2 min read | by Jordi Prats

If you try to add a golang module that is on a private repository you'll get an error similar to this:

$ go get github.com/pet2cattle/golang-demo
go: downloading github.com/pet2cattle/golang-demo v0.0.0-20220925191817-0b4b7026fa7f
go: github.com/pet2cattle/golang-demo@v0.0.0-20220925191817-0b4b7026fa7f: verifying module: github.com/pet2cattle/golang-demo@v0.0.0-20220925191817-0b4b7026fa7f: reading https://sum.golang.org/lookup/github.com/pet2cattle/golang-demo@v0.0.0-20220925191817-0b4b7026fa7f: 404 Not Found
        server response:
        not found: github.com/pet2cattle/golang-demo@v0.0.0-20220925191817-0b4b7026fa7f: invalid version: git ls-remote -q origin in /tmp/gopath/pkg/mod/cache/vcs/75d68059a7355b978972dea177e930262ce90abe410680b8db8a45a587e02c26: exit status 128:
                fatal: could not read Username for 'https://github.com': terminal prompts disabled
        Confirm the import path was entered correctly.
        If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.

To be able to use modules hosted on private repositories you'll have to setup some environment variables with the private organization and a Personal Access Token with enough permissions to read the repository:



export GOPRIVATE=github.com/<private org>
export GOPROXY=direct
git config --global url."https://<PAT>@github.com/".insteadOf https://github.com/



So for example, assuming the organization is pet2cattle and the PAT is ghp_AAAABBBCCCCCDDDDDDDDDFFFFFFFGGGGGGGHH, the resulting commands would be:

$ export GOPRIVATE=github.com/pet2cattle
$ export GOPROXY=direct
$ git config --global url."https://ghp_AAAABBBCCCCCDDDDDDDDDFFFFFFFGGGGGGGHH@github.com/".insteadOf https://github.com/

Once setup, the first time you try to retrieve it might (depends on how the organization is configured) request to use the browser to validate it:

$ go get github.com/pet2cattle/golang-demo
go: module github.com/pet2cattle/golang-demo: git ls-remote -q origin in /Users/pet2cattle/go/pkg/mod/cache/vcs/75d68059a7355b978972dea177e930262ce90abe410680b8db8a45a587e02c26: exit status 128:
  remote: The `pet2cattle' organization has enabled or enforced SAML SSO. To access
  remote: this repository, visit https://github.com/enterprises/pet2cattle/sso?authorization_request=AAZGOYLODFDFNZ2GSCLMLT3IVKFG5XBPLSFSCKB5AHS5GC8DJNDEAD2LEZYCEM27UVVR6EZLEM4XHI2LBNR3WSZGOH2EHACF2WGY3FONM4ZGKZ4D8VNHWA6TLDFADCHA5PNC5LUNBAZQ
  remote: and try your request again.
  fatal: unable to access 'https://github.com/pet2cattle/golang-demo/': The requested URL returned error: 403

Once we have validated the PAT it will just download the private repositories:

$ go get github.com/pet2cattle/golang-demo
go: downloading github.com/pet2cattle/golang-demo v0.0.0-20220925191817-0b4b7026fa7f
go: added github.com/pet2cattle/golang-demo v0.0.0-20220925191817-0b4b7026fa7f

Posted on 26/09/2022

Categories