What's the proper way to "go get" a private repository?

2 min read 07-10-2024
What's the proper way to "go get" a private repository?


Unlocking Private Repositories: A Guide to "go get" with Private Code

The "go get" command is a powerful tool for developers using Go, allowing them to fetch and install packages from public repositories. But what about private repositories? How can you use "go get" to bring your own private code into your Go project?

Scenario: Imagine you have a private Go project hosted on GitHub, GitLab, or a similar platform. You want to use this project as a dependency in another Go application. The standard "go get" won't work because your repository isn't publicly accessible.

Original Code (Won't Work):

go get github.com/your-username/your-private-project

The Solution: Authentication and SSH Keys

The key is to provide "go get" with the necessary credentials to access your private repository. This usually involves using SSH keys or a personal access token.

Step 1: Generate SSH Keys

If you haven't already, generate an SSH key pair on your local machine:

ssh-keygen -t rsa -b 4096 -C "[email protected]"

Step 2: Add Public Key to Repository

Copy the contents of the public key file (usually id_rsa.pub) and add it to your repository's settings. This allows your machine to authenticate with the repository over SSH.

Step 3: Configure Git Remote URL

In your local Go project, configure the remote URL to use the SSH protocol. Replace [email protected]:your-username/your-private-project.git with your actual repository URL:

git remote set-url origin [email protected]:your-username/your-private-project.git

Step 4: Use "go get" with SSH

Now you can use "go get" with the SSH URL:

go get [email protected]:your-username/your-private-project.git

Important Considerations:

  • SSH Keys: Ensure your SSH keys are properly protected and avoid sharing them publicly.
  • Access Permissions: Verify that the user associated with the SSH key has access to the private repository.
  • Private Modules: If you are working on a larger project with multiple private modules, consider using a module proxy or setting up a private Go module registry for more efficient management.

Alternative: Using a Personal Access Token

Instead of SSH keys, you can use a personal access token generated from your repository provider's website.

  1. Create a Token: Go to your repository provider's settings and generate a personal access token with appropriate permissions (e.g., read/write access).
  2. Configure Environment Variables: Set the GOPRIVATE environment variable to your private repository's URL and GOINSECURE to 1 to allow fetching from non-standard domains.
  3. Use "go get" with Token: Run "go get" as usual, and the token will be used for authentication.

Example using GitHub and Personal Access Token:

export GOPRIVATE="github.com/your-username/*"
export GOINSECURE=1
go get github.com/your-username/your-private-project

Conclusion

Utilizing SSH keys or personal access tokens allows you to seamlessly integrate private code into your Go projects. By following these steps, you can efficiently manage your private dependencies and leverage the full potential of "go get".

Further Resources: