March 23, 2021

GOLANG: Get modules from private registry

How the add dependencies via “go get” from private registry

In this short article I want to show you how to configure your environment to install modules from a private repository from gitlab or github using “go get”. Note: I have tested this only on linux and mac os. I think it should work in a similar way on Windows.

Broadly speaking, this requires two steps:

  • Generate an API key for your gitlab or github account
  • Customize the .netrc file in your home directory

The Module

Our golang module which we will install later has a simple function here in the example.

package Playgroud

import "fmt"

// Play returns a greeting string with the playername
func Play(name string) string {
    return fmt.Sprintf("hello, %s! \n", name)
}

And the go.mod file looks like follow:

module github.com/AICDEV/playground

go 1.16

We push the code to our private repository. In this example github.

The Project

Now we create a project in which we want to install and manage our module “Playground” as a dependency via “go get”.

Here is our project code:

package main

import (
    "fmt"
)

func main() {
    fmt.Println("I greetz some users")
}

Installing our private dependencies

Now let’s try to install our modules without making any changes to our system.

If we now try to install the module via:

go get https://github.com/AICDEV/playground

We end up with the following error message:

So, first thing is to update/edit the url. Let’s try again:

go get github.com/AICDEV/playground

And again, we end up in an error. But this time the error message indicates that we try to get a module from a private repository.

Now we need to set two things.

Creating the APIKEY

Using github as an example, I’ll show you how to create an apikey. You need to navigate in the following way: “Account -> Settings > Developer Settings -> Personal access tokens”

It is necessary to add at least “repo” access as a permisson for you apikey. With Gitlab the APIKEY generation works similar. You copy the token. Gitlab and Github show the token only once!

Creating the .netrc file

What exactly does the .netrc file do?

“The .netrc file contains login and initialization information used by the auto-login process. It generally resides in the user’s home directory, but a location outside of the home directory can be set using the environment variable NETRC.” read more here.

We adding the following lines to our .netrc file:

machine github.com
     login aicdev
     password 347608ccfab7402652076d4fc5d2cce40e002efc

Now we are able to install our private dependency.

Update our project

Let’s run our install command again:

go get github.com/AICDEV/playground

This time we went through without any error message. Let’s test our code. But first have a look at the updated go mod file in our test project.

module aicdev.com/test

go 1.16

require github.com/AICDEV/playground v0.0.0-20210322205625-3cdcf81e9c59 // indirect

Looks good this time!

Then use our new dependency:

package main

import (
    "fmt"

    Playground "github.com/AICDEV/playground"
)

func main() {
    fmt.Println("I greetz some users")
    fmt.Println(Playground.Play("douglas adams"))
}

And start the program

go run test.go

Summary

I hope you had fun and learned a way to install private go modules.

close

Leave a Reply

Your email address will not be published. Required fields are marked *