DEV Community

Cover image for How to "go get" from private repository
Tirumal Rao
Tirumal Rao

Posted on

How to "go get" from private repository

Installing a go package is breeze with the introduction of go mod. However, when it comes to getting (installing) packages from private repository then it's not that straight forward.

In this post, I'll discuss how to go get from a private repository. I'll show how to get packages from Github.com, but method is almost same for other repository hosting services.

OPERATING SYSTEM: Mac or Linux

STEP 1: Create SSH Key

In this step I'll generate SSH key, in my case I've multiple Github Accounts. One for is personal and another one for my organization I'm currently working with.

Please make sure that you give it a unique file name when asked.

ssh-keygen
Enter fullscreen mode Exit fullscreen mode

Enter a name and select the default location ~/.ssh

Enter file in which to save the key (/Users/<username>/.ssh/id_rsa):
Enter fullscreen mode Exit fullscreen mode

STEP 2: SSH Config file

This step is optional. I'm creating a config file to use multiple SSH keys for multiple GitHub/Bitbucket/Gitlab accounts.

CREATE(if not exist) a file config in ~/.ssh and enter Host configuration for particular SSH keys and Github account.

Here is a sample:

# Bitbucket Account
Host acme
    HostName bitbucket.org
    User git
    IdentityFile ~/.ssh/acme
    IdentitiesOnly yes

# Bitbucket Account
Host simpson
    HostName bitbucket.org
    User git
    IdentityFile ~/.ssh/simpson
    IdentitiesOnly yes

# GitHub Account
Host donald
    HostName github.com
    User git
    IdentityFile ~/.ssh/donald
    IdentitiesOnly yes
Enter fullscreen mode Exit fullscreen mode

STEP 3: Create a Global Git Config

On you terminal enter following command and execute

git config --global url.git@acme:<GITHUB_USER_NAME>.insteadOf https://github.com/<GITHUB_USER_NAME>
Enter fullscreen mode Exit fullscreen mode

Here, acme is the Host configuration from ~/.ssh/config file.

Replace, with your Github username.

If done correctly, the file at location ~/.gitconfig will be modified. The above configuration will added to the EOF.

STEP 4: Now go get 😊

go get github.com/<GITHUB_USER_NAME>/private-repo
Enter fullscreen mode Exit fullscreen mode

Top comments (0)