{"record":{"id":"bda9c4017f423813","repo":"golang/go","slug":"expecting-a-go-version-like-q","errorCode":null,"errorMessage":"expecting a Go version like %q","messagePattern":"expecting a Go version like %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/modcmd/tidy.go","lineNumber":96,"sourceCode":"\tbase.AddModCommonFlags(&cmdTidy.Flag)\n}\n\n// A goVersionFlag is a flag.Value representing a supported Go version.\n//\n// (Note that the -go argument to 'go mod edit' is *not* a goVersionFlag.\n// It intentionally allows newer-than-supported versions as arguments.)\ntype goVersionFlag struct {\n\tv string\n}\n\nfunc (f *goVersionFlag) String() string { return f.v }\nfunc (f *goVersionFlag) Get() any       { return f.v }\n\nfunc (f *goVersionFlag) Set(s string) error {\n\tif s != \"\" {\n\t\tlatest := gover.Local()\n\t\tif !modfile.GoVersionRE.MatchString(s) {\n\t\t\treturn fmt.Errorf(\"expecting a Go version like %q\", latest)\n\t\t}\n\t\tif gover.Compare(s, latest) > 0 {\n\t\t\treturn fmt.Errorf(\"maximum supported Go version is %s\", latest)\n\t\t}\n\t}\n\n\tf.v = s\n\treturn nil\n}\n\nfunc runTidy(ctx context.Context, cmd *base.Command, args []string) {\n\tmoduleLoader := modload.NewLoader()\n\tif len(args) > 0 {\n\t\tbase.Fatalf(\"go: 'go mod tidy' accepts no arguments\")\n\t}\n\n\t// Tidy aims to make 'go test' reproducible for any package in 'all', so we\n\t// need to include test dependencies. For modules that specify go 1.15 or","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/modcmd/tidy.go#L78-L114","documentation":"Thrown by goVersionFlag.Set when the provided Go version string does not match `modfile.GoVersionRE`. This regex validates the structural format of a Go version (e.g., '1.22', '1.22.1'). The flag is used by `go mod tidy -go` to set or update the go directive in go.mod. An invalid format like 'v1.22' or '1.x' or 'go1.22' fails the regex.","triggerScenarios":"Running `go mod tidy -go=v1.22` (with a 'v' prefix), `go mod tidy -go=1` (too short), `go mod tidy -go=go1.22` (with 'go' prefix), or `go mod tidy -go=latest` (non-numeric). The regex GoVersionRE expects a pattern like `\\d\\.\\d\\d?(\\.\\d\\d?)?`.","commonSituations":"Confusing module version syntax (v1.2.3) with Go toolchain version syntax (1.22); adding a 'v' prefix by habit; adding 'go' prefix because that's how it appears in go.mod; using a major.minor only like '1' instead of '1.22'.","solutions":["Use bare major.minor format without 'v' or 'go' prefix: `go mod tidy -go=1.22`","Include patch version if needed: `go mod tidy -go=1.22.1`","Check the latest supported version with `go version` and use that as a reference"],"exampleFix":"// before\ngo mod tidy -go=v1.22\n// after\ngo mod tidy -go=1.22","handlingStrategy":"validation","validationCode":"import (\n    \"regexp\"\n    \"golang.org/x/mod/modfile\"\n)\n\nfunc validateGoVersionFlag(s string) error {\n    if !modfile.GoVersionRE.MatchString(s) {\n        return fmt.Errorf(\"%q is not a valid Go version (use format like 1.22)\", s)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use bare major.minor format (1.22) — no 'v' or 'go' prefix","Reference `go version` output for the correct format","In scripts, construct the flag value programmatically rather than hardcoding"],"tags":["go","go-mod","version","regex-validation","cli-argument"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}