Back
Featured image of post Using Go Modules with private repositories at Github

Using Go Modules with private repositories at Github

A tiny set of code snippets to allow developers to use go mod easily with private repositories

Table of Content

Unlike many programming languages, Go distributes modules from repositories instead of a central package server. One benefit of this approach is that publishing a private module is very similar to publishing a public one. Instead of requiring a completely separate private package server, a Go private module is distributed via a private source code repository. Since most source code hosting options support this out of the box, there’s no need to set up an additional private server.

In order to use a private module, you’ll need to have access to a private Go module. In this section, you’ll create and publish a private module you can use later in the tutorial to access a private module from another Go program.

Problem description

I have a private repo/module that I own, that I want to use in my project that I’m working on locally. Running ‘go mod download’ or ‘tidy’ returns an error because it cannot access this repo (understandably). What is an appropriate way of accessing this repo?

You can see original issue at StackOverflow at https://stackoverflow.com/questions/52209721/go-mod-private-repo

To ‘fix’ the issue of downloading go modules from private repositories, there are some alternatives we can use:

  • Alternative 1: Configure our local git client to download go modules with SSH instead of HTTP.
  • Alternative 2: Configure your local $HOME/.netrc file with a Git access token for private repos.

Alternative 1: Configure your system to use go mod with SSH

First step is to configure the project to tell go mod tool to download the private go modules using SSH connections instead of HTTP. This will allow an easier authentication with public-private key scheme. To apply this change, developers need to modify their .gitconfig

1
2
[url "ssh://git@github.com/"]
   insteadOf = https://github.com/

Flag your private repos as private

Next step is to configure your Go project to set which dependencies are considered to be private. To do so, we can use the Go environment variable GOPRIVATE. For example, to configure all my github repos to be considered private, I can use the following command to set the environment variable.

1
go env -w GOPRIVATE=github.com/zerjioang

After those steps, I can execute go mod again to download dependencies

1
go mod tidy

Alternative 2: Configure your local $HOME/.netrc

Previous solutions tends to work in most Linux environments, but more elegant solution is using a ~/.netrc file. This file defines automatic logins for ftp, but other programs have also adopted it, including Git.

If your system does not include this file, you can create it with touch $HOME/.netrc. The file should have the following format:

1
machine gitlab.company.com login USERNAME password TOKEN

Replace gitlab.company.com, USERNAME, and TOKEN with your values. Now when you push or pull via HTTPS in Git, it will use that information to login you in. This is why you need the write_repository scope for local purposes.

This will also work with GitHub, Bitbucket, or GitLab.com and even private Gitlab VCS repositories.

1
machine github.com login USERNAME password TOKEN

One final thing that you’ll have to do is set a GOPRIVATE environment variable. This contains a comma-separated list of module prefixes. Save this value to your ~/.bashrc or ~/.zshrc.

References



💬 Share this post in social media

Thanks for checking this out and I hope you found the info useful! If you have any questions, don't hesitate to write me a comment below. And remember that if you like to see more content on, just let me know it and share this post with your colleges, co-workers, FFF, etc.

You are free to use the content, but mention the author (@MrSergioAnguita) and link the post!
Last updated on Jan 14, 2022 10:59 CET
Please, don't try to hack this website servers. Guess why...