Fix Private Repository Access in Golang

February 19, 2025

When working with private Go repositories, proper configuration is needed for successful access. Here's how to set it up correctly.

Configuration Steps

Git Configuration

First, configure Git to use SSH instead of HTTPS:

git config --global url."[email protected]_github_company:companyhub/privaterepo.git".insteadOf "https://github.com/companyhub/privaterepo"

Go Private Configuration

Configure Go to recognize private repositories:

export GOPRIVATE=github.com/companyhub/*

Permanent Configuration

Add to your shell configuration file:

echo "export GOPRIVATE=github.com/companyhub/*" >> ~/.bashrc

Clean and Update

Reset your Go modules cache:

go clean -modcache
go mod tidy

Key Points

  • SSH configuration must be properly set up
  • GOPRIVATE environment variable tells Go to skip public proxy
  • Git configuration redirects HTTPS to SSH
  • Module cache should be cleaned after configuration
#Golang #Git #SSH #Configuration #Tutorial