Go get private repository: step-by-step guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Go get for a private repository: why is it a pain for beginners?



You wrote a cool Go package, uploaded it to GitHub in a private repository, and now want to use it in another project. You run go get github.com/yourname/secret-lib — and get an error 403 Forbidden or remote: Repository not found. Sound familiar?



This situation is one of the most common pitfalls for beginner Go developers. The Stack Overflow question "What's the proper way to 'go get' a private repository?" has thousands of views. The problem is that go get uses HTTPS by default, not SSH, and doesn't know your credentials.



In this article, we'll break down three working methods to configure go get for private repositories: via .gitconfig, .netrc, and the GOPRIVATE environment variable. You'll learn how to avoid errors and save hours of debugging.



Why doesn't go get work with private repositories?



By default, go get tries to download a module via HTTPS. For public repositories, this is fine — anonymous access works. But private repositories require authentication. Go doesn't know how to automatically provide a login/password or token.



The second problem is the module discovery mechanism. Go first checks if the repository exists on GitHub/GitLab/Bitbucket by analyzing the HTML page. For private repositories, this page is unavailable without authorization. Hence the repository not found error, even though the repository exists.



Method 1: edit ~/.gitconfig (the simplest)



The easiest way is to force Git to use SSH instead of HTTPS for a specific domain. Add the following lines to the ~/.gitconfig file:



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


Now any request to https://github.com/ will be automatically replaced with SSH. Make sure you have an SSH key configured and added to your GitHub/GitLab account.



Let's check:

go get github.com/your-org/private-repo# Works without errors!


Important: this method changes the behavior for all repositories on GitHub. If you only need it for one, use a more precise setting:



[url "git@github.com:your-org/"]    insteadOf = https://github.com/your-org/


Method 2: .netrc for tokens and passwords



If you prefer HTTPS (for example, in CI/CD), use the ~/.netrc file. Go can read this file and provide credentials automatically.



Create the ~/.netrc file with permissions 600:



machine github.comlogin your-github-usernamepassword your-personal-access-token


Where to get the token? On GitHub: Settings → Developer settings → Personal access tokens → Generate new token. Select the repo scope (for private repositories).



After that:

go get github.com/your-org/private-repo


For GitLab:

machine gitlab.comlogin your-gitlab-usernamepassword your-gitlab-access-token


This method is ideal for CI/CD, where SSH keys are not always convenient to store.



Method 3: GOPRIVATE — a lifesaver for modules



The GOPRIVATE environment variable tells Go: "Hey, these modules are private, don't try to check them through the public proxy and don't visit their HTML pages." This solves the "repository not found" problem.



Set it in the terminal (or add to ~/.bashrc / ~/.zshrc):



export GOPRIVATE=github.com/your-org/*


Now Go will ignore the public module proxy (proxy.golang.org) for all repositories in your organization. The go get command will become

Blogs

Book Recommendations