golang/go · error

expecting a Go version like %q

Error message

expecting a Go version like %q

What it means

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.

Source

Thrown at src/cmd/go/internal/modcmd/tidy.go:96

	base.AddModCommonFlags(&cmdTidy.Flag)
}

// A goVersionFlag is a flag.Value representing a supported Go version.
//
// (Note that the -go argument to 'go mod edit' is *not* a goVersionFlag.
// It intentionally allows newer-than-supported versions as arguments.)
type goVersionFlag struct {
	v string
}

func (f *goVersionFlag) String() string { return f.v }
func (f *goVersionFlag) Get() any       { return f.v }

func (f *goVersionFlag) Set(s string) error {
	if s != "" {
		latest := gover.Local()
		if !modfile.GoVersionRE.MatchString(s) {
			return fmt.Errorf("expecting a Go version like %q", latest)
		}
		if gover.Compare(s, latest) > 0 {
			return fmt.Errorf("maximum supported Go version is %s", latest)
		}
	}

	f.v = s
	return nil
}

func runTidy(ctx context.Context, cmd *base.Command, args []string) {
	moduleLoader := modload.NewLoader()
	if len(args) > 0 {
		base.Fatalf("go: 'go mod tidy' accepts no arguments")
	}

	// Tidy aims to make 'go test' reproducible for any package in 'all', so we
	// need to include test dependencies. For modules that specify go 1.15 or

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Use bare major.minor format without 'v' or 'go' prefix: `go mod tidy -go=1.22`
  2. Include patch version if needed: `go mod tidy -go=1.22.1`
  3. Check the latest supported version with `go version` and use that as a reference

Example fix

// before
go mod tidy -go=v1.22
// after
go mod tidy -go=1.22
Defensive patterns

Strategy: validation

Validate before calling

import (
    "regexp"
    "golang.org/x/mod/modfile"
)

func validateGoVersionFlag(s string) error {
    if !modfile.GoVersionRE.MatchString(s) {
        return fmt.Errorf("%q is not a valid Go version (use format like 1.22)", s)
    }
    return nil
}

Prevention

When it happens

Trigger: 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?)?`.

Common situations: 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'.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/bda9c4017f423813. Report an issue: GitHub.