This ultimately requires a couple of simple steps; however, as there are some confusing instructrions (and both GPT and Meta.ai get confused on the issue) I figured I’d lay them down here.

The go command ultimately uses git to download packages as listed in its go.mod file; however, I found that modifying the .git/config file as suggested in several places does not seem to yield good results.

What I found to work is to do the following two steps (note that just one or the other won’t be sufficient, you need them both).

  1. tell go how to authenticate to GitHub (I recommend using a Personal Access Token, as opposed to using your password – mostly, as I feel extremely uncomfortable in typing my password, any password in cleartext); and
  2. tell go which repositories should be considered private.

Adding Credentials

Step #1 is accomplished by adding this line to your ~/.netrc file (which go consults):

machine github.com login <user> password ghp_1j234fae..

Here <user> is your GitHub login username (most likely, the email you use at GitHub: someone@example.com); the password is a Personal Access Token – keep it to yourself and secure, also remember to give it a reasonably short lifespan (say, 90 days) and remember to renew it.

Telling Go About Your Private Repository

This is relatively easy, and is accomplished by using the GOPRIVATE env variable:

$ go env -w GOPRIVATE="github.com/alertavert"

As the documentation states

The variable is a comma-separated list of glob patterns (in the syntax of Go’s path.Match) of module path prefixes.

Profit

And that’s pretty much all there is to it; you can then use go get or go mod tidy with modules that live in private repos.

Happy Gophing!

Leave a comment

Trending