Imports #
"golang.org/x/mod/modfile"
"bytes"
"strings"
"internal/gover"
"internal/goversion"
"runtime"
"strconv"
"sort"
"strings"
"golang.org/x/mod/module"
"golang.org/x/mod/semver"
"cmd/go/internal/base"
"context"
"errors"
"fmt"
"strings"
"golang.org/x/mod/modfile"
"bytes"
"strings"
"internal/gover"
"internal/goversion"
"runtime"
"strconv"
"sort"
"strings"
"golang.org/x/mod/module"
"golang.org/x/mod/semver"
"cmd/go/internal/base"
"context"
"errors"
"fmt"
"strings"
DefaultGoModVersion is the Go version to assume for go.mod files that do not declare a Go version. The go command has been writing go versions to modules since Go 1.12, so a go.mod without a version is either very old or recently hand-written. Since we can't tell which, we have to assume it's very old. The semantics of the go.mod changed at Go 1.17 to support graph pruning. If see a go.mod without a go line, we have to assume Go 1.16 so that we interpret the requirements correctly. Note that this default must stay at Go 1.16; it cannot be moved forward.
const DefaultGoModVersion = "1.16"DefaultGoWorkVersion is the Go version to assume for go.work files that do not declare a Go version. Workspaces were added in Go 1.18, so use that.
const DefaultGoWorkVersion = "1.18"var ErrTooNew = *ast.CallExprExplicitIndirectVersion is the Go version at which a module's go.mod file is expected to list explicit requirements on every module that provides any package transitively imported by that module. Other indirect dependencies of such a module can be safely pruned out of the module graph; see https://golang.org/ref/mod#graph-pruning.
const ExplicitIndirectVersion = "1.17"ExplicitModulesTxtImportVersion is the Go version at which vendored packages need to be present in modules.txt to be imported.
const ExplicitModulesTxtImportVersion = "1.23"goStrictVersion is the Go version at which the Go versions became "strict" in the sense that, restricted to modules at this version or later, every module must have a go version line ≥ all its dependencies. It is also the version after which "too new" a version is considered a fatal error.
const GoStrictVersion = "1.21"narrowAllVersion is the Go version at which the module-module "all" pattern no longer closes over the dependencies of tests outside of the main module.
const NarrowAllVersion = "1.16"separateIndirectVersion is the Go version at which "// indirect" dependencies are added in a block separate from the direct ones. See https://golang.org/issue/45965.
const SeparateIndirectVersion = "1.17"Startup records the information that went into the startup-time version switch. It is initialized by switchGoToolchain.
var Startup struct{...}TestVersion is initialized in the go command test binary to be $TESTGO_VERSION, to allow tests to override the go command's idea of its own version as returned by Local.
var TestVersion stringtidyGoModSumVersion is the Go version at which 'go mod tidy' preserves go.mod checksums needed to build test dependencies of packages in "all", so that 'go test all' can be run without checksum errors. See https://go.dev/issue/56222.
const TidyGoModSumVersion = "1.21"var nl = *ast.CallExprA Switcher provides the ability to switch to a new toolchain in response to TooNewErrors. See [cmd/go/internal/toolchain.Switcher] for documentation.
type Switcher interface {
Error(err error)
Switch(ctx context.Context)
}A TooNewError explains that a module is too new for this version of Go.
type TooNewError struct {
What string
GoVersion string
Toolchain string
}Compare returns -1, 0, or +1 depending on whether x < y, x == y, or x > y, interpreted as toolchain versions. The versions x and y must not begin with a "go" prefix: just "1.21" not "go1.21". Malformed versions compare less than well-formed versions and equal to each other. The language version "1.21" compares less than the release candidate and eventual releases "1.21rc1" and "1.21.0".
func Compare(x string, y string) intfunc (e *TooNewError) Error() stringFromGoMod returns the go version from the go.mod file. It returns DefaultGoModVersion if the go.mod file does not contain a go line or if mf is nil.
func FromGoMod(mf *modfile.File) stringFromGoWork returns the go version from the go.mod file. It returns DefaultGoWorkVersion if the go.mod file does not contain a go line or if wf is nil.
func FromGoWork(wf *modfile.WorkFile) stringFromToolchain returns the Go version for the named toolchain, derived from the name itself (not by running the toolchain). A toolchain is named "goVERSION". A suffix after the VERSION introduced by a -, space, or tab is removed. Examples: FromToolchain("go1.2.3") == "1.2.3" FromToolchain("go1.2.3-bigcorp") == "1.2.3" FromToolchain("invalid") == ""
func FromToolchain(name string) stringGoModLookup takes go.mod or go.work content, finds the first line in the file starting with the given key, and returns the value associated with that key. Lookup should only be used with non-factored verbs such as "go" and "toolchain", usually to find versions or version-like strings.
func GoModLookup(gomod []byte, key string) stringfunc (e *TooNewError) Is(err error) boolIsLang reports whether v denotes the overall Go language version and not a specific release. Starting with the Go 1.21 release, "1.x" denotes the overall language version; the first release is "1.x.0". The distinction is important because the relative ordering is 1.21 < 1.21rc1 < 1.21.0 meaning that Go 1.21rc1 and Go 1.21.0 will both handle go.mod files that say "go 1.21", but Go 1.21rc1 will not handle files that say "go 1.21.0".
func IsLang(x string) boolIsPrerelease reports whether v denotes a Go prerelease version.
func IsPrerelease(x string) boolIsToolchain reports whether the module path corresponds to the virtual, non-downloadable module tracking go or toolchain directives in the go.mod file. Note that IsToolchain only matches "go" and "toolchain", not the real, downloadable module "golang.org/toolchain" containing toolchain files. IsToolchain("go") = true IsToolchain("toolchain") = true IsToolchain("golang.org/x/tools") = false IsToolchain("golang.org/toolchain") = false
func IsToolchain(path string) boolIsValid reports whether the version x is valid.
func IsValid(x string) boolLang returns the Go language version. For example, Lang("1.2.3") == "1.2".
func Lang(x string) stringLocal returns the local Go version, the one implemented by this go command.
func Local() stringLocalToolchain returns the local toolchain name, the one implemented by this go command.
func LocalToolchain() stringMax returns the maximum of x and y interpreted as toolchain versions, compared using Compare. If x and y compare equal, Max returns x.
func Max(x string, y string) stringModCompare returns the result of comparing the versions x and y for the module with the given path. The path is necessary because the "go" and "toolchain" modules use a different version syntax and semantics (gover, this package) than most modules (semver).
func ModCompare(path string, x string, y string) intModIsPrefix reports whether v is a valid version syntax prefix for the module with the given path. The caller is assumed to have checked that ModIsValid(path, vers) is true.
func ModIsPrefix(path string, vers string) boolModIsPrerelease reports whether v is a prerelease version for the module with the given path. The caller is assumed to have checked that ModIsValid(path, vers) is true.
func ModIsPrerelease(path string, vers string) boolModIsValid reports whether vers is a valid version syntax for the module with the given path.
func ModIsValid(path string, vers string) boolModMajorMinor returns the "major.minor" truncation of the version v, for use as a prefix in "@patch" queries.
func ModMajorMinor(path string, vers string) stringModSort is like module.Sort but understands the "go" and "toolchain" modules and their version ordering.
func ModSort(list []module.Version)Prev returns the Go major release immediately preceding v, or v itself if v is the first Go major release (1.0) or not a supported Go version. Examples: Prev("1.2") = "1.1" Prev("1.3rc4") = "1.2"
func Prev(x string) stringToolchainMax returns the maximum of x and y interpreted as toolchain names, compared using Compare(FromToolchain(x), FromToolchain(y)). If x and y compare equal, Max returns x.
func ToolchainMax(x string, y string) stringfunc local() (goVers string, toolVers string)func maybeToolchainVersion(name string) stringfunc parseKey(line []byte, key string) (string, bool)Generated with Arrow