TExcelTAD
Home › Go Modules in Depth › Private Modules & Proxies

Private Modules & Proxies

By default the go command fetches every module from the public mirror and checks it against the public checksum database:

text
GOPROXY=https://proxy.golang.org,direct
GOSUMDB=sum.golang.org

That's wrong for company code in two ways: the public proxy can't see your private repositories, and asking the checksum database about git.corp.example/payments would leak the name of your private module to a public service. Four environment variables control this.

The variables

Variable Meaning Example
GOPROXY comma-separated list of proxies tried in order; direct = fetch from the VCS (Git) yourself; off = no downloads https://goproxy.corp.example,https://proxy.golang.org,direct
GOPRIVATE glob patterns of private module path prefixes; the default for the two below *.corp.example,github.com/acme-private
GONOPROXY modules fetched direct instead of through GOPROXY (defaults to GOPRIVATE) none when your proxy serves private modules
GONOSUMDB modules never looked up in the checksum database (defaults to GOPRIVATE)
GOAUTH (Go 1.24+) how to authenticate to proxies and VCS hosts (netrc by default, git, or a command) git /home/me/src

The patterns are path prefixes matched element by element with Go's path.Match syntax: *.corp.example matches git.corp.example/team/lib, because its first element matches. Set them once with go env -w GOPRIVATE=*.corp.example (stored in go env GOENV's file), or in CI's environment.

A model of the decision

For each module the go command asks two questions: through the proxy or direct, and check the checksum database or not. This model implements the matching rule:

js · try it yourself
// Go's rule: a pattern matches if it matches a leading run of path elements (path.Match per element).
function globMatch(pattern, element) {
  const re = new RegExp('^' + pattern.split('*').map((s) => s.replace(/[.+?^${}()|[\]\\]/g, '\\
amp;')).join('[^/]*') + ' ); return re.test(element); } function matchesAny(globs, modPath) { const elems = modPath.split('/'); return globs.split(',').filter(Boolean).some((g) => { const parts = g.split('/'); return parts.length <= elems.length && parts.every((p, i) => globMatch(p, elems[i])); }); } function decide(modPath, env) { const noproxy = env.GONOPROXY ?? env.GOPRIVATE ?? ''; const nosumdb = env.GONOSUMDB ?? env.GOPRIVATE ?? ''; const via = noproxy !== 'none' && matchesAny(noproxy, modPath) ? 'direct (VCS)' : env.GOPROXY; const sum = nosumdb !== 'none' && matchesAny(nosumdb, modPath) ? 'go.sum only' : 'sum.golang.org'; return `${modPath.padEnd(28)} via ${via.padEnd(28)} checked by ${sum}`; } const mods = ['rsc.io/quote', 'git.corp.example/payments', 'github.com/acme-private/ui', 'github.com/acme/public']; const setups = { 'GOPRIVATE only': { GOPROXY: 'https://proxy.golang.org', GOPRIVATE: '*.corp.example,github.com/acme-private' }, 'company proxy': { GOPROXY: 'https://goproxy.corp.example', GOPRIVATE: '*.corp.example,github.com/acme-private', GONOPROXY: 'none' }, }; for (const [name, env] of Object.entries(setups)) { console.log(`# ${name}`); for (const m of mods) console.log(decide(m, env)); }
# GOPRIVATE only
rsc.io/quote                 via https://proxy.golang.org     checked by sum.golang.org
git.corp.example/payments    via direct (VCS)                 checked by go.sum only
github.com/acme-private/ui   via direct (VCS)                 checked by go.sum only
github.com/acme/public       via https://proxy.golang.org     checked by sum.golang.org
# company proxy
rsc.io/quote                 via https://goproxy.corp.example checked by sum.golang.org
git.corp.example/payments    via https://goproxy.corp.example checked by go.sum only
github.com/acme-private/ui   via https://goproxy.corp.example checked by go.sum only
github.com/acme/public       via https://goproxy.corp.example checked by sum.golang.org
  1. GOPRIVATE only: private modules are cloned straight from Git (your Git credentials must work) and are never sent to the checksum database. Public ones still use the public proxy.
  2. Company proxy (Athens, Artifactory, Nexus, GitHub Packages…) that can read your private repositories: GONOPROXY=none sends everything through it, while GOPRIVATE still keeps private names away from sum.golang.org. This is the setup go help private recommends for companies.

Both setups, for real

The container has no Git server, so the session plays company: it publishes corp.example/auth into a private proxy directory with the gopublish helper from Upgrades, exclude & retract, and lists that proxy first in GOPROXY, before the public one.

bash · try it yourself
# gopublish DIR MODULE VERSION PROXYDIR: write one module version in GOPROXY layout
mkdir -p ~/tools/gopublish && cat > ~/tools/gopublish/main.go <<'SRC'
package main

import (
	"archive/zip"
	"os"
	"path/filepath"
)

func main() {
	dir, mod, ver := os.Args[1], os.Args[2], os.Args[3]
	at := filepath.Join(os.Args[4], mod, "@v")
	os.MkdirAll(at, 0o755)
	gomod, _ := os.ReadFile(filepath.Join(dir, "go.mod"))
	os.WriteFile(filepath.Join(at, ver+".mod"), gomod, 0o644)
	info := `{"Version":"` + ver + `","Time":"2026-01-15T10:00:00Z"}`
	os.WriteFile(filepath.Join(at, ver+".info"), []byte(info), 0o644)
	f, _ := os.Create(filepath.Join(at, ver+".zip"))
	z := zip.NewWriter(f)
	filepath.WalkDir(dir, func(p string, d os.DirEntry, err error) error {
		if !d.IsDir() {
			rel, _ := filepath.Rel(dir, p)
			w, _ := z.Create(mod + "@" + ver + "/" + filepath.ToSlash(rel))
			data, _ := os.ReadFile(p)
			w.Write(data)
		}
		return nil
	})
	z.Close()
	f.Close()
	list, _ := os.OpenFile(filepath.Join(at, "list"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
	list.WriteString(ver + "\n")
	list.Close()
}
SRC
(cd ~/tools/gopublish && go mod init gopublish 2>/dev/null && go build -o ~/go/bin/gopublish .)
mkdir -p auth && cd auth
go mod init corp.example/auth 2>/dev/null
printf 'package auth\n\nfunc Check(user string) bool { return user == "ada" }\n' > auth.go
cd ..
gopublish auth corp.example/auth v1.0.0 corp-proxy
mkdir app && cd app
go mod init example.com/app 2>/dev/null
cat > main.go <<'SRC'
package main

import (
	"fmt"

	"corp.example/auth"
	"rsc.io/quote"
)

func main() { fmt.Println(auth.Check("ada"), quote.Go()) }
SRC
export GOPROXY=file://$HOME/corp-proxy,file:///opt/goproxy
echo "--- 1. GOPRIVATE alone: private modules go direct, to a Git host we don't have"
GOPRIVATE=corp.example go get corp.example/auth@v1.0.0 2>&1 | sed 's/: dial tcp.*/: (no network here)/'
echo "--- 2. company proxy: GONOPROXY=none sends everything through GOPROXY"
export GOPRIVATE=corp.example GONOPROXY=none
go env GOPRIVATE GONOPROXY GONOSUMDB
go mod tidy
go run .
grep corp go.sum
--- 1. GOPRIVATE alone: private modules go direct, to a Git host we don't have
go: corp.example/auth@v1.0.0: unrecognized import path "corp.example/auth": https fetch: Get "https://corp.example/auth?go-get=1": (no network here)
--- 2. company proxy: GONOPROXY=none sends everything through GOPROXY
corp.example
none
corp.example
go: finding module for package rsc.io/quote
go: finding module for package corp.example/auth
go: downloading corp.example/auth v1.0.0
go: downloading rsc.io/quote v1.5.2
go: found corp.example/auth in corp.example/auth v1.0.0
go: found rsc.io/quote in rsc.io/quote v1.5.2
go: downloading rsc.io/sampler v1.3.0
go: downloading golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
true Don't communicate by sharing memory, share memory by communicating.
corp.example/auth v1.0.0 h1:pGv9zSlALIC7ySjNiwgUdj8NVB2aYjlpHiulvkhQjEk=
corp.example/auth v1.0.0/go.mod h1:fFEgLVnKeuBLuL/yJPsgV9vM3W7XOIosPAKLTZw1DqY=

The first attempt shows exactly what GOPRIVATE means: the go command skipped the proxies and tried https://corp.example/auth?go-get=1, the lookup that finds a module's Git repository (the sed shortens the network error). The second setup downloads corp.example/auth from the private proxy. Its hash is still recorded in go.sum, so private modules get the same tamper protection after the first download; only the public database lookup is skipped.

What a module proxy is

Any HTTPS server that answers these GET requests is a GOPROXY:

Request Answer
$base/$module/@v/list known versions, one per line
$base/$module/@v/$version.info JSON: {"Version": "v1.0.0", "Time": "…"}
$base/$module/@v/$version.mod the go.mod file
$base/$module/@v/$version.zip the module zip
$base/$module/@latest (optional) .info of the latest version

Self-hosted options: Athens (open source, proxies public modules and fetches private ones with its own credentials), Artifactory, Nexus and GitHub Packages-style registries, or a plain static file server holding a directory like the one gopublish writes. With a caching company proxy, builds keep working when GitHub is down or a public repository disappears.

Note: For private Git hosts without a proxy, the go command runs git itself: configure credentials with ~/.netrc, a Git credential helper, or git config --global url."ssh://git@git.corp.example/".insteadOf "https://git.corp.example/". GOINSECURE allows plain HTTP for a pattern, and GOVCS restricts which version control tools may be used; avoid widening either.

Test yourself

What does GOPRIVATE=*.corp.example do by default?

  1. Only hides the modules from pkg.go.dev
  2. Sets GONOPROXY and GONOSUMDB: those modules are fetched directly from VCS and never checked against sum.golang.org
  3. Encrypts the module cache
  4. Makes go.sum optional

Your company proxy can serve private modules. Which setting sends them through it while keeping their names away from the public checksum database?

  1. GOPRIVATE=*.corp.example with GONOPROXY=none
  2. GOSUMDB=off
  3. GOPROXY=direct
  4. GOFLAGS=-insecure

Are private modules protected by go.sum?

  1. No, go.sum only covers public modules
  2. Yes: their hashes are recorded and checked like any other; only the checksum database lookup is skipped
  3. Only if vendored
  4. Only with GOSUMDB=off