What Is the Default Target Go Version for the go-modern-guidelines CLI?
When executed without explicit version flags, the go-modern-guidelines CLI defaults to the local Go toolchain version obtained from go env GOVERSION.
The go-modern-guidelines CLI analyzes Go code against specific language versions to enforce modern best practices. When you run the tool without specifying a target version, it automatically detects the Go version installed on your local machine. This default detection mechanism ensures that the CLI aligns with your current development environment without requiring manual configuration.
How the CLI Determines the Default Go Version
The resolution logic resides in the goversion package and follows a clear fallback chain. When both the --go-version flag and a module file path are omitted, the CLI invokes the goversion.Resolve function with empty parameters.
In internal/goversion/goversion.go (lines 62-67), the Resolve function checks if both filePath and goVersion arguments are empty strings. When this condition is met, it immediately delegates to resolveGoToolVersion to query the local Go installation.
The Local Toolchain Query
The resolveGoToolVersion function, defined at lines 63-71 in internal/goversion/goversion.go, executes the following shell command to retrieve the host's Go version:
go env GOVERSION
This command returns the version string of the Go binary currently installed on the system (for example, go1.22.5 or devel for development builds). The function then normalizes this output to produce a standardized version identifier used throughout the analysis.
CLI Entry Point
The command-line interface parses user input in internal/cli/cli.go (lines 79-84). When no --go-version flag is provided and no path to a go.mod or go.work file is specified, the CLI passes empty strings to the resolution function, triggering the local toolchain detection path.
Code Implementation Details
The following simplified Go code illustrates how the default version resolution flows through the system:
// Resolution triggered from CLI parsing
targetVersion, err := goversion.Resolve(
"", // filePath: not supplied
"", // goVersion: no --go-version flag
guidelines.LatestKnownVersion(), // fallback reference
)
// Inside goversion.Resolve (lines 62-67):
if filePath == "" && goVersion == "" {
return resolveGoToolVersion()
}
// Inside resolveGoToolVersion (lines 63-71):
output, err := exec.Command("go", "env", "GOVERSION").Output()
normalized, err := normalizeGoVersion(string(output), develVersion)
return normalized, nil
In this flow, targetVersion ultimately contains the normalized version string from your local Go installation (e.g., "1.22"), which the CLI then uses to filter and apply modern Go guidelines.
Version Resolution Hierarchy
The CLI evaluates the target version in the following priority order:
- Explicit
--go-versionflag — Uses the version string provided by the user - Module file detection — If a
go.modorgo.workpath is provided, extracts the declared version - Local toolchain — Executes
go env GOVERSIONto detect the host's installed Go version
Only when options 1 and 2 are absent does the system fall back to option 3, the local toolchain query.
Summary
- The default target Go version for go-modern-guidelines is the version returned by
go env GOVERSIONon the host machine. - Resolution occurs in
internal/goversion/goversion.gothrough thegoversion.Resolvefunction when both file paths and version flags are omitted. - The specific function
resolveGoToolVersion(lines 63-71) executes the toolchain query and normalizes the result. - CLI parsing in
internal/cli/cli.go(lines 79-84) initiates this resolution when no explicit version configuration is present. - Normalization handles various version string formats, including development builds.
Frequently Asked Questions
How does go-modern-guidelines determine the default Go version?
When no version is specified via flags or module files, the CLI executes go env GOVERSION through the resolveGoToolVersion function in internal/goversion/goversion.go. This retrieves and normalizes the version string of the locally installed Go toolchain, using it as the analysis target.
What happens if I don't specify the --go-version flag?
If you omit the --go-version flag and don't provide a path to a go.mod file, the CLI automatically detects your local Go version. The goversion.Resolve function detects the empty parameters and calls resolveGoToolVersion, which queries your system's Go installation via the go env command.
Can I override the default target version?
Yes, you can override the automatic detection by either passing the --go-version flag with a specific version string or by providing the path to a go.mod or go.work file. When either option is present, the CLI uses that explicit version instead of querying the local toolchain.
What Go version does the CLI use if it cannot detect the local toolchain?
The analysis indicates that guidelines.LatestKnownVersion() serves as a fallback reference passed to the resolution function, though the primary default mechanism relies on go env GOVERSION. If the local toolchain query fails, the system has access to this latest known version constant as a reference point, though the specific error handling behavior depends on the implementation details in internal/goversion/goversion.go.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →