Home  >  Article  >  Backend Development  >  Setting up and utilizing private modules in Go

Setting up and utilizing private modules in Go

Patricia Arquette
Patricia ArquetteOriginal
2024-10-22 17:16:03976browse

Setting up and utilizing private modules in Go

Getting started with private modules in Go, can become a daunting task without the right resources to reference. This is so as Go modules are not stored or served from a central repository but they can be pulled from different repositories; an example being Github.

Importing public Go packages to your project can be as easy as running a single command:

$ go get github.com/author/module-name

On the other end setting up and using private modules in Go will require more steps. With private modules you can host private packages in your repository of choice and use it without necessarily making the code publicly accessible.

To set up a private module, begin by creating a directory and navigate to it. Initialize the module by running the following command:

$ go mod init github.com/author/module-name

Next step is to commit, add a tag for the commit which aids in versioning the module and push the module to a repository, Github in this case.

In this example the module is the root of your project, but there are cases where another go module is the root and you could have submodules. With submodules, the above command would translate to below:

$ go mod init github.com/author/root-module/module-name

Fetching a Private Repository
The module is now hosted in a repository but it is private, hence it requires authentication before one can pull and make use of it in their projects. Projects can access the module by authenticating using local environment variables, through a Github action or a dockerfile.

In this article we will focus on accessing a private repository while utilizing local environment variables. This can be achieved with the following two options.

In both of these two options, it is a requirement to set the GOPRIVATE environment variable. GOPRIVATE environment variable helps the Go command determine whether the module being fetched is public or private. It contains patterns that are checked against repository package names and any package name that matches this variable will not be fetched via the public Go cache server.

Set and export the GOPRIVATE environment variable by running the commands below in the module directory.

$ export GOPRIVATE=github.com/author/module-name

Alternatively, you can use the Go env command to set the GOPRIVATE variable as below:

$ go env -w GOPRIVATE=github.com/author/module-name
$ go env GOPRIVATE

*Configuring git to fetch via SSH instead of HTTP(S)
*

The Go get command uses http or https to fetch modules from git. To ensure that it uses ssh to pull the module, you can do that by editing the git config file or by using this command:

$ go env -w GOPRIVATE=github.com/author/module-name
$ go env GOPRIVATE

*Configuring Go with a Personal Access Token
*

With the GOPRIVATE variable set, this option requires you to set and export two other variables.

GITHUB_ACCESS_TOKEN variable set to a Personal Access Token, which you can create from the Github settings: Personal Access Token. Ensure to give the token a name and select the repo on the scope section.

The other variable to set and export is the GONOPROXY variable set to localhost highlighting that this url should not be compared against the checksum database.

$ git config --global url."git@github.com:author/module-name".insteadOf "https://github.com/author/module-name"

Finally, update the global git config to use the generated personal token.

$ export GONOPROXY=localhost 
$ export GITHUB_ACCESS_TOKEN=<your-token>

With these configurations in place, you should be able to now fetch the private module with either of the above options and use it in your project. To fetch the module, run the go run . command to build the project which in turns pulls the module as it’s added one of the project’s dependencies or by running the go get command specifying the module github path.

The above is the detailed content of Setting up and utilizing private modules in Go. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn