gotoolchain

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const DefaultMinVersion = "1.27.3"

DefaultMinVersion is the minimum Go version required by the project templates.

View Source
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

func BinaryForEnv(name string, env []string) string

BinaryForEnv resolves "go" using PATH from env first, falling back to the cached toolchain Binary. Non-"go" executable names are returned unchanged.

func Command

func Command(args ...string) *exec.Cmd

Command returns an *exec.Cmd configured to run the resolved Go executable with an enriched PATH.

func CommandContext

func CommandContext(ctx context.Context, args ...string) *exec.Cmd

CommandContext returns an *exec.Cmd configured to run the resolved Go executable with ctx and an enriched PATH.

func CompareVersion

func CompareVersion(a, b string) int

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

func EnrichedEnvWith(baseEnv []string, resolvedBin string, env SearchEnv) []string

EnrichedEnvWith returns a copy of baseEnv whose PATH includes resolvedBin's directory, existing PATH entries, and any existing standard Go/tool directories.

func Env

func Env(baseEnv []string) []string

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

func EnvWith(baseEnv []string, extra ...string) []string

EnvWith returns Env(baseEnv) with extra KEY=VALUE entries applied (replacing matching keys).

func MaxGoVersion

func MaxGoVersion(versions ...string) string

MaxGoVersion returns the highest valid Go version among versions, or empty if none are valid.

func ParseGoModMinVersion

func ParseGoModMinVersion(goMod []byte) (string, bool)

ParseGoModMinVersion extracts the `go X.Y[.Z]` directive version from a go.mod or go.mod.tmpl file.

func ParseGoVersion

func ParseGoVersion(output string) (string, bool)

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

func ResolveBinaryIn(env SearchEnv) (string, bool)

ResolveBinaryIn locates a `go` executable in env by checking:

  1. env.PathEnv
  2. Standard Go install locations, Homebrew, and version-manager shims
  3. 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

func StandardGoBinDirs(env SearchEnv) []string

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

func ProbeIn(env SearchEnv, minVersion string) Status

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

func RefreshMin(minVersion string) Status

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

func ResolveMin(minVersion string) Status

ResolveMin returns the cached Go toolchain probe evaluated against minVersion (DefaultMinVersion when empty).

func (Status) OK

func (s Status) OK() bool

OK reports whether a Go binary was found, responded to `go version`, and satisfies MinVersion.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL