Keywords: Go language | GOPRIVATE | private modules
Abstract: This article provides an in-depth exploration of the GOPRIVATE environment variable in Go, addressing the 410 Gone error when accessing private modules. By analyzing the Go module system's architecture, it details how to configure GOPRIVATE to bypass public proxies and checksum databases, ensuring secure access to private code. The guide covers basic configuration, wildcard usage, persistent settings, and supplementary SSH configurations, offering a complete solution for Go developers managing private dependencies.
Go Module System and Private Code Access Mechanism
In Go 1.13 and later versions, the module system has become the standard approach for dependency management. When using commands like go run, go build, or go get, the Go toolchain attempts by default to download modules from public proxy servers (e.g., proxy.golang.org) and validate their integrity through checksum databases (sum.golang.org). This mechanism ensures the security and consistency of public code but causes access failures for code in private repositories.
Analysis of the 410 Gone Error
When attempting to access private modules, a common error message appears:
verifying github.com/repoURL/go-proto@v2.86.0+incompatible/go.mod: reading https://sum.golang.org/lookup/github.com/!repoURL/go-proto@v2.86.0+incompatible: 410 Gone
This error indicates that the Go toolchain is trying to validate the private module through the public checksum database. Since the module is not in a public repository, the server returns a 410 Gone status code. Even with correctly configured SSH keys allowing direct git clone operations, the Go module system still follows the default proxy and validation process.
Core Function of the GOPRIVATE Environment Variable
The GOPRIVATE environment variable is key to resolving this issue. It defines a pattern list of module path prefixes, instructing the Go toolchain which modules should be treated as private, thereby bypassing public proxies and checksum databases. Its syntax is based on Go's path.Match pattern matching rules, supporting wildcards and comma-separated lists of multiple patterns.
Practical Methods for Configuring GOPRIVATE
For specific private repositories, use the following command for configuration:
go env -w GOPRIVATE=github.com/repoURL/private-repo
This command uses go env -w to write the configuration to Go's environment variable settings, ensuring it applies to all subsequent Go commands. To configure all repositories within an entire organization, use a wildcard pattern:
go env -w GOPRIVATE=github.com/<OrgNameHere>/*
Here, <OrgNameHere> should be replaced with the actual organization name, and the /* wildcard matches all modules under that organization.
Multi-Pattern Configuration and Syntax Details
GOPRIVATE supports configuring multiple private module patterns, for example:
GOPRIVATE=*.corp.example.com,rsc.io/private
This configuration marks *.corp.example.com and rsc.io/private and all their subpaths as private. Pattern matching follows these rules:
*matches any sequence of non-separator characters?matches any single non-separator character[abc]matches any character in the specified set
Supplementary Configuration for SSH Access
For private Git servers accessed via SSH protocol, URL rewrite rules may need to be added to ~/.gitconfig. For example, for an internal Git server:
[url "ssh://git@git.local.intranet/"]
insteadOf = https://git.local.intranet/
This configuration ensures that when the Go toolchain attempts to access git.local.intranet via HTTPS, it actually uses the SSH protocol for connection, leveraging the configured SSH keys for authentication.
Configuration Verification and Troubleshooting
After configuration, verify the GOPRIVATE setting with:
go env GOPRIVATE
If configured correctly, this command outputs the set private module patterns. If access issues persist, check:
- Whether the module path exactly matches the
GOPRIVATEpattern - Whether Git configuration is correct, especially SSH keys and URL rewrite rules
- Network connectivity to the private Git server
Security Considerations and Best Practices
While GOPRIVATE resolves private module access issues, security implications should be noted:
- Private modules do not undergo integrity validation through public checksum databases
- Ensure robust access control and authentication mechanisms for private Git servers
- In team collaboration environments, standardize
GOPRIVATEconfiguration to avoid inconsistencies
For enterprise applications, consider combining GONOPROXY and GONOSUMDB environment variables for finer control, or use private proxy servers to manage all module dependencies.