Easiest way
GOPRIVATE=github.com/PlanToPack/api-utils go get github.com/PlanToPack/api-utils@latest
Final implementation
git config --global url."https://:@github.com/PlanToPack/".insteadOf "https://github.com/PlanToPack/"
Steps to Configure Your Private Go Module
-
Repository Setup (GitHub):
-
Ensure your Go code is in a dedicated GitHub repository.
-
Private Repository: Make sure the repository is set to "Private" in your GitHub repository settings.
-
Go Module Initialization: Inside your repository, initialize a Go module. If you haven't already, run:
Bash
go mod init github.com/your-github-username/your-private-repo-name- Replace
your-github-usernameandyour-private-repo-namewith your actual GitHub username and repository name.
- Replace
-
Commit and Push: Commit your
go.modfile and push the changes to your GitHub repository.
-
-
Authentication Setup:
- Personal Access Token (PAT):
- Go to your GitHub settings -> Developer settings -> Personal access tokens -> Tokens (classic).
- Click "Generate new token (classic)".
- Give your token a descriptive name (e.g., "Go Module Access").
- Crucially, select the
reposcope (orread:packagesif you are using packages). This grants the necessary permissions to access private repositories. - Copy the generated token and store it securely. You won't be able to see it again.
- Personal Access Token (PAT):
-
Configuring Go to Use the PAT:
~/.netrcor~/_netrcFile:-
Create a file named
.netrc(or_netrcon Windows) in your home directory. -
Add the following lines to the file, replacing the placeholders with your GitHub username and PAT:
machine github.com login your-github-username password your-personal-access-token -
Important: Set the file permissions to restrict access:
Bash
chmod 600 ~/.netrc # or chmod 600 ~/_netrc on windows using git bash.- This ensures that only your user can read the file.
-
-
Importing the Private Module in Another Project:
-
go getorgo mod tidy: In your other Go project, usego getorgo mod tidyto add the private module as a dependency:Bash
go get github.com/your-github-username/your-private-repo-name- or if you are working on a project that already has dependencies, and you added the import statement to your go code, use:
Bash
go mod tidy -
Import in Go Code: In your Go code, import the module using the same import path:
Go
import "github.com/your-github-username/your-private-repo-name"
-