search
HomeBackend DevelopmentGolangSetting up and utilizing private modules in Go

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>
</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
Golang: The Go Programming Language ExplainedGolang: The Go Programming Language ExplainedApr 10, 2025 am 11:18 AM

The core features of Go include garbage collection, static linking and concurrency support. 1. The concurrency model of Go language realizes efficient concurrent programming through goroutine and channel. 2. Interfaces and polymorphisms are implemented through interface methods, so that different types can be processed in a unified manner. 3. The basic usage demonstrates the efficiency of function definition and call. 4. In advanced usage, slices provide powerful functions of dynamic resizing. 5. Common errors such as race conditions can be detected and resolved through getest-race. 6. Performance optimization Reuse objects through sync.Pool to reduce garbage collection pressure.

Golang's Purpose: Building Efficient and Scalable SystemsGolang's Purpose: Building Efficient and Scalable SystemsApr 09, 2025 pm 05:17 PM

Go language performs well in building efficient and scalable systems. Its advantages include: 1. High performance: compiled into machine code, fast running speed; 2. Concurrent programming: simplify multitasking through goroutines and channels; 3. Simplicity: concise syntax, reducing learning and maintenance costs; 4. Cross-platform: supports cross-platform compilation, easy deployment.

Why do the results of ORDER BY statements in SQL sorting sometimes seem random?Why do the results of ORDER BY statements in SQL sorting sometimes seem random?Apr 02, 2025 pm 05:24 PM

Confused about the sorting of SQL query results. In the process of learning SQL, you often encounter some confusing problems. Recently, the author is reading "MICK-SQL Basics"...

Is technology stack convergence just a process of technology stack selection?Is technology stack convergence just a process of technology stack selection?Apr 02, 2025 pm 05:21 PM

The relationship between technology stack convergence and technology selection In software development, the selection and management of technology stacks are a very critical issue. Recently, some readers have proposed...

How to use reflection comparison and handle the differences between three structures in Go?How to use reflection comparison and handle the differences between three structures in Go?Apr 02, 2025 pm 05:15 PM

How to compare and handle three structures in Go language. In Go programming, it is sometimes necessary to compare the differences between two structures and apply these differences to the...

How to view globally installed packages in Go?How to view globally installed packages in Go?Apr 02, 2025 pm 05:12 PM

How to view globally installed packages in Go? In the process of developing with Go language, go often uses...

What should I do if the custom structure labels in GoLand are not displayed?What should I do if the custom structure labels in GoLand are not displayed?Apr 02, 2025 pm 05:09 PM

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...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),