gover: support Semantic Versioning major versions beyond 1

For #64033

Change-Id: Iab132f86c66aa6115a349d8032e9766a14dad02e
Reviewed-on: https://go-review.googlesource.com/c/go/+/541915
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
This commit is contained in:
lotusirous 2023-11-13 22:03:09 +07:00 committed by Gopher Robot
parent a9c9cc07ac
commit 71fc9d4da5
2 changed files with 10 additions and 2 deletions

View File

@ -7,7 +7,10 @@
// [Go versions]: https://go.dev/doc/toolchain#version
package version // import "go/version"
import "internal/gover"
import (
"internal/gover"
"strings"
)
// stripGo converts from a "go1.21" version to a "1.21" version.
// If v does not start with "go", stripGo returns the empty string (a known invalid version).
@ -33,7 +36,11 @@ func Lang(x string) string {
if v == "" {
return ""
}
return x[:2+len(v)] // "go"+v without allocation
if strings.HasPrefix(x[2:], v) {
return x[:2+len(v)] // "go"+v without allocation
} else {
return "go" + v
}
}
// Compare returns -1, 0, or +1 depending on whether

View File

@ -48,6 +48,7 @@ var langTests = []testCase1[string, string]{
{"go1.2.3", "go1.2"},
{"go1.2", "go1.2"},
{"go1", "go1"},
{"go222", "go222.0"},
{"go1.999testmod", "go1.999"},
}