SoFunction
Updated on 2025-04-13

Go mod private repository configuration summary

Golang version control

go version v1.22.0

When we create a new onegoWhen the project is executed in the project root directorygo mod initFiles can be initialized for managing package dependencies. Subsequent executiongo mod tidyUpdate and download all dependencies.
If we do not configure, we can only access the public open source repository. If you want to download a private repository dependency package, you need to make some configuration.

go mod uses private repository (gitlab) dependency settings

If you want to configure a hosting site, follow the steps below:

Set the GOPRIVATE environment variable to prevent proxying:

  • go env -w GOPRIVATE=*./*
  • go env -w GONOPROXY=*./*

There are two ways to configure repository access:

  • Method 1 (recommended): Configure git to convert requests from ssh to https: git config --global url."ssh://git@/".insteadof "/"
  • Method 2: Add repository access to the Token: git config --global "PRIVATE-TOKEN: YOUR_PRIVATE_TOKEN"

Since the verification server cannot be accessed, choose one of the following methods:

  • Method 1: go env -w GONOSUMDB=/*
  • Method 2: Close the verification service go env -w GOSUMDB=off

If the private library does not support the https protocol, set skip CA certificate verification: go env -w GOINSECURE='/*'

After the configuration is completed,git config -lTo view the git configuration, you can also edit the git configuration directly through .gitconfig:

[http]
	extraheader = PRIVATE-TOKEN:UsQsxy5q_Nqy-pXHtu-V
	
[url "ssh://git@/"]
	insteadof = /

Bygo envTo view the environment variable configuration, you can also configure the environment variables directly through the profile:

export GOPRIVATE=*./*
export GONOPROXY=*./*
export GOINSECURE=*./*
export GONOSUMDB=*./*

Configure code hosting site

Go mod finds code repository principle

/cmd/go#hdr-Remote_import_paths

If the import path is not a known code hosting site and also lacks version control qualifiers, the go tool will try to get the import through https/http and in the document'sHTML <head>Find in<meta>mark.

The meta tag is as follows:

<meta name="go-import" content="import-prefix vcs repo-root">

When using mod, it will be recognizedgo-importAdditional variants of meta tags and priority is higher than those default version control systems. This variant usesmodAs the content valuevcs, as follows:

<meta name="go-import" content=" mod /moduleproxy">

This tag indicates from URL/moduleproxyGet the path in the module proxy toThe module at the beginning. For more information about the proxy protocol, see /ref/mod#goproxy-protocol.

Find the repository for module path: /ref/mod#vcs-find

Use proxy to implement code hosting site access

Generally, mainstream code hosting sites such as github, gitlab, Gitee, etc. all support returning golang mod metadata to the site by default.
If the domain name of the site cannot be exposed to the public, and you cannot even have permission to change the site's access address. For example, when deploying a site,ip+portaccess in a way. At this time, you need to configure a virtual domain name as the reference package path of the go mod. At this time, you can use Nginx to configure the site's intranet access proxy to return a static page with a meta tag. For example, to visit a site

if ($args ~* "^go-get=1") {
                        set $condition goget;
        }
        if ($uri ~ "(/[^/]+/[^/]+)(/.*)?") {
                        set $condition "${condition}path";
        }
        if ($condition = gogetpath) {
                        return 200 "<!DOCTYPE html><html><head><meta content='$1 git $' name='go-import'></head></html>";
        }

refer to:/questions/26347516/using-go-get-on-a-personal-git-repo

This is the end of this article about the configuration summary of go mod private warehouse. For more related content on go mod private warehouse configuration, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!