Skip to content

go/version: package for handling Go version strings #62039

@mvdan

Description

@mvdan

go.mod files have had language version directives for a while, like go 1.20. In Go 1.21, they started allowing more specific versions such as go 1.21.0 or go 1.21rc3.

Some tools like https://github.com/mvdan/gofumpt want to fetch the language version from go.mod, to for example see if a module can use new syntax in Go 1.21 or not. This was easy enough when the format was go 1.X, where X is an integer. Now it's harder - a bit of code would have to be written to consider the trailing patch release number, pre-release suffixes like RCs or betas, etc.

In #61692 (comment), @findleyr and I seemed to agree that we should expose some API to deal with these version strings, since they are particular to Go and don't follow a standard like semver.

For example, how about a function to allow converting a Go version to semver:

package modfile

func SemverVersion(version string) string

Then, SemverVersion("1.21rc3") == "v1.21-rc.3", for example. It could also accept a go prefix, like go1.21rc3, since that is used in some places like git tag names - those don't appear in go.mod files, but I think it would be useful too.

Also see mvdan/gofumpt#280 for the issue on gofumpt's side - it for now continues to panic if it sees any "complex" Go version it can't deal with.

cc @dominikh since I think he also needs something like this API for staticcheck.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions