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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

Under the BeegoORM framework, how to specify the database associated with the model? Many Beego projects require multiple databases to be operated simultaneously. When using Beego...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...
