-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go/version provides basic comparison of Go versions, for use when deciding whether certain language features are allowed, and so on. See the proposal issue #62039 for more details. Fixes #62039 Change-Id: Ibdfd4fe15afe406c46da568cb31feb42ec30b530 Reviewed-on: https://go-review.googlesource.com/c/go/+/538895 Auto-Submit: Russ Cox <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
- Loading branch information
Showing
9 changed files
with
545 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pkg go/version, func Compare(string, string) int #62039 | ||
pkg go/version, func IsValid(string) bool #62039 | ||
pkg go/version, func Lang(string) string #62039 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2023 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// Package version provides operations on [Go versions]. | ||
// | ||
// [Go versions]: https://go.dev/doc/toolchain#version | ||
package version // import "go/version" | ||
|
||
import "internal/gover" | ||
|
||
// 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). | ||
func stripGo(v string) string { | ||
if len(v) < 2 || v[:2] != "go" { | ||
return "" | ||
} | ||
return v[2:] | ||
} | ||
|
||
// Lang returns the Go language version for version x. | ||
// If x is not a valid version, Lang returns the empty string. | ||
// For example: | ||
// | ||
// Lang("go1.21rc2") = "go1.21" | ||
// Lang("go1.21.2") = "go1.21" | ||
// Lang("go1.21") = "go1.21" | ||
// Lang("go1") = "go1" | ||
// Lang("bad") = "" | ||
// Lang("1.21") = "" | ||
func Lang(x string) string { | ||
v := gover.Lang(stripGo(x)) | ||
if v == "" { | ||
return "" | ||
} | ||
return x[:2+len(v)] // "go"+v without allocation | ||
} | ||
|
||
// Compare returns -1, 0, or +1 depending on whether | ||
// x < y, x == y, or x > y, interpreted as Go versions. | ||
// The versions x and y must begin with a "go" prefix: "go1.21" not "1.21". | ||
// Invalid versions, including the empty string, compare less than | ||
// valid versions and equal to each other. | ||
// The language version "go1.21" compares less than the | ||
// release candidate and eventual releases "go1.21rc1" and "go1.21.0". | ||
// Custom toolchain suffixes are ignored during comparison: | ||
// "go1.21.0" and "go1.21.0-bigcorp" are equal. | ||
func Compare(x, y string) int { | ||
return gover.Compare(stripGo(x), stripGo(y)) | ||
} | ||
|
||
// IsValid reports whether the version x is valid. | ||
func IsValid(x string) bool { | ||
return gover.IsValid(stripGo(x)) | ||
} |
Oops, something went wrong.