Digital Sanctum

Personal blog of Shane Witbeck Tags · Archive · About

How to use go mod with private GitHub repositories

Published: 17 Aug 2021

If you have one or more dependencies in a private GitHub repository the following instructions are one way to allow git mod download to succeed.

Step 1: Pass repository credentials to Go module

git config --global url."https://${GITHUB_USERNAME}:${GITHUB_TOKEN}@github.com".insteadOf "https://github.com"

where GITHUB_USERNAME and GITHUB_TOKEN are your GitHub username and personal access token values respectively.

Step 2: Bypass Go Module proxy

Set the GOPRIVATE environment variable scoped to a user or organization

go env -w GOPRIVATE=github.com/digitalsanctum

or scope to a specific GitHub repository:

go env -w GOPRIVATE=github.com/digitalsanctum/foo

Multiple values are separated by a comma.

For more information, see the documentation.