Documentation
¶
Overview ¶
Package gotoolchain locates the Go toolchain executable and enriches child process environments when the editor or CLI is launched from a GUI desktop launcher (Finder, Dock, Spotlight, Windows shortcut, or Linux desktop launcher) that inherits only a minimal system PATH.
Index ¶
- Constants
- func Binary() string
- func BinaryForEnv(name string, env []string) string
- func Command(args ...string) *exec.Cmd
- func CommandContext(ctx context.Context, args ...string) *exec.Cmd
- func CompareVersion(a, b string) int
- func EnrichedEnvWith(baseEnv []string, resolvedBin string, env SearchEnv) []string
- func Env(baseEnv []string) []string
- func EnvWith(baseEnv []string, extra ...string) []string
- func MaxGoVersion(versions ...string) string
- func ParseGoModMinVersion(goMod []byte) (string, bool)
- func ParseGoVersion(output string) (string, bool)
- func ResetCache()
- func ResolveBinaryIn(env SearchEnv) (string, bool)
- func SetSearchEnvForTest(env SearchEnv) func()
- func StandardGoBinDirs(env SearchEnv) []string
- type SearchEnv
- type Status
Constants ¶
const DefaultMinVersion = "1.27.3"
DefaultMinVersion is the minimum Go version required by the project templates.
const DownloadURL = "https://go.dev/dl/"
DownloadURL is the official Go download page linked when Go is missing or too old.
Variables ¶
This section is empty.
Functions ¶
func Binary ¶
func Binary() string
Binary returns the cached resolved path to `go`, or "go" when no binary was found.
func BinaryForEnv ¶
BinaryForEnv resolves "go" using PATH from env first, falling back to the cached toolchain Binary. Non-"go" executable names are returned unchanged.
func Command ¶
Command returns an *exec.Cmd configured to run the resolved Go executable with an enriched PATH.
func CommandContext ¶
CommandContext returns an *exec.Cmd configured to run the resolved Go executable with ctx and an enriched PATH.
func CompareVersion ¶
CompareVersion compares two Go versions (with or without a leading "go" prefix) using Go's canonical version ordering (go/version.Compare). Returns -1 if a < b, 0 if a == b, and +1 if a > b.
func EnrichedEnvWith ¶
EnrichedEnvWith returns a copy of baseEnv whose PATH includes resolvedBin's directory, existing PATH entries, and any existing standard Go/tool directories.
func Env ¶
Env returns a copy of baseEnv (or os.Environ() when baseEnv is nil) whose PATH includes the resolved Go binary's directory and standard Go install directories.
func EnvWith ¶
EnvWith returns Env(baseEnv) with extra KEY=VALUE entries applied (replacing matching keys).
func MaxGoVersion ¶
MaxGoVersion returns the highest valid Go version among versions, or empty if none are valid.
func ParseGoModMinVersion ¶
ParseGoModMinVersion extracts the `go X.Y[.Z]` directive version from a go.mod or go.mod.tmpl file.
func ParseGoVersion ¶
ParseGoVersion extracts a Go version string (without the leading "go" prefix, e.g. "1.27.4") from `go version` output such as "go version go1.27.4 darwin/arm64" or "go version devel go1.28-abcdef0123 Thu Nov 27 ...".
func ResetCache ¶
func ResetCache()
ResetCache clears the cached Go toolchain and login-shell PATH discovery results.
func ResolveBinaryIn ¶
ResolveBinaryIn locates a `go` executable in env by checking:
- env.PathEnv
- Standard Go install locations, Homebrew, and version-manager shims
- Login shell PATH on Unix hosts
func SetSearchEnvForTest ¶
func SetSearchEnvForTest(env SearchEnv) func()
SetSearchEnvForTest replaces the process-wide SearchEnv used by Resolve, Refresh, Command, and Env, clears the cached probe result, and returns a function that restores the previous state.
func StandardGoBinDirs ¶
StandardGoBinDirs returns the ordered standard Go install, package-manager, and version-manager directories probed when `go` is absent from a GUI launcher's minimal PATH.
Types ¶
type SearchEnv ¶
type SearchEnv struct {
GOOS string
PathEnv string
HomeDir string
GoRoot string
ProgramFiles string
LocalAppData string
PathExt string
RootDir string
LoginShellPATH func() string
VersionOf func(ctx context.Context, binary string, env []string) (string, error)
}
SearchEnv captures host environment parameters used to discover the Go toolchain. Tests populate it directly (including RootDir and LoginShellPATH) without mutating process state.
func DefaultSearchEnv ¶
func DefaultSearchEnv() SearchEnv
DefaultSearchEnv returns the host process environment for Go toolchain discovery.
type Status ¶
type Status struct {
// Found reports whether a `go` executable was located.
Found bool
// Binary is the resolved path to the `go` executable, or empty when Found is false.
Binary string
// Version is the Go version reported by `go version` without the leading "go" prefix (for example "1.27.4").
Version string
// MinVersion is the minimum Go version checked against (for example "1.27.3").
MinVersion string
// TooOld reports whether Found is true and Version is older than MinVersion.
TooOld bool
// PATH is the resolved PATH string containing Binary's directory and standard tool directories.
PATH string
// Err holds any error encountered while running `go version`.
Err string
}
Status describes the result of probing for a usable Go toolchain.
func ProbeIn ¶
ProbeIn discovers `go` using env, runs `go version`, and checks it against minVersion.
func Refresh ¶
func Refresh() Status
Refresh clears the cached Go toolchain and login-shell PATH results, re-probes, and evaluates against DefaultMinVersion.
func RefreshMin ¶
RefreshMin clears the cached Go toolchain and login-shell PATH results, re-probes, and evaluates against minVersion.
func Resolve ¶
func Resolve() Status
Resolve locates Go once per process and evaluates it against DefaultMinVersion.
func ResolveMin ¶
ResolveMin returns the cached Go toolchain probe evaluated against minVersion (DefaultMinVersion when empty).