Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MassiGy/1d0fde7574b126c3c45c6b6f66200fab to your computer and use it in GitHub Desktop.
Save MassiGy/1d0fde7574b126c3c45c6b6f66200fab to your computer and use it in GitHub Desktop.

How to import remote private modules using go toolchain?




In this tutorial, we will explain how you can use shared golang code by importing remote gitlab repos to your current project. For this tutorial, we assume that the gitlab instance is a private one, and it is hosted by a company internally. So all the shared modules are private from the go toolchain standpoint. By the same token, this tutorial will only benefit developers working for that given company. And since I am working for Renault, the examples will be derived from what I see/use at work.

First of all, as a developer make sure to connect to your vpn if you are working remotely.




Let's assume that we have a local project :

    > ls 
    - project/

    > ls project/
    - main.go go.mod


And in our main.go we want to use a remote shared module. For instance, let's use https://gitlabee.intra.com/shared-modules/ip4-errors

as an example. Through the ip4errors package, we should get access to methods like GetErrorMessage.



To get this remote module we need to follow a couple of steps. First we need to update our $HOME/.netrc (%USERPROFILE%/_netrc on windows), so as go toolscan access gitlabee.

Append this to your netrc file.

    machine gitlabee.intra.com
    login firstname.lastname 
    password <your personal access token>


If you are running windows, you can copy the entire file into _netrc to keep them in-sync. Consider checking/backing up this latter to prevent any config loss.

    cp ~/.netrc ~/_netrc    #for windows


Once we've done that, we need to make sure that our git is proprely setup so as all the git requests are authenicated. This is especially important if you want to access modules that are nested inside subgroups.

       # cache our credentials - for convenience 
       git config --global credential.helper cache
   
       # tell git to always authenicate our requests - otherwise go get won't work
       git config --global url."https://<FIRSTNAME.LASTNAME>:<GITLAB_ACCESS_TOKEN>@gitlabee.intra.com".insteadOf "https://gitlabee.intra.com"    
       
       vim ~/.gitconfig 
   
   
       # then download with go get the remote go module (our shared code)
       GONOSUMDB=gitlabee.intra.com GONOPROXY=gitlabee.intra.com go get gitlabee.intra.com/shared-modules/ip4-errors
   
       # NOTE ! 
       # As you can see, we omit the GOSUMDB and the GOPROXY for this go get since we do not have any setup internally.
 
 
       # finally, edit the code to use the shared module and run it.
       vim main.go    
      
      # import the package of this shared module   
       import ( 
           "http://gitlabee.intra.com/ip4-errors/ip4errors"  
       )  
 
       # use the methods GetErrorMessage in your code with the package's name
       
       tst := ip4errors.GetErrorMessage(ip4errors.IP4_1_field_assemblySite_is_mandatory)   
       
       # And run it.    
       go run main.go      
       
       # NOTE ! 
       # A module can contain one or more packages
   



References :

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment