Skip to content

Commit 8a042a3

Browse files
committed
fix: parsing of the Go version
1 parent 0c8e63e commit 8a042a3

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

helpers.go

+21-5
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ import (
3434
// initialization only imports.
3535
//
3636
// Usage:
37-
// node, matched := MatchCallByPackage(n, ctx, "math/rand", "Read")
3837
//
38+
// node, matched := MatchCallByPackage(n, ctx, "math/rand", "Read")
3939
func MatchCallByPackage(n ast.Node, c *Context, pkg string, names ...string) (*ast.CallExpr, bool) {
4040
importedName, found := GetImportedName(pkg, c)
4141
if !found {
@@ -474,9 +474,25 @@ func RootPath(root string) (string, error) {
474474

475475
// GoVersion returns parsed version of Go from runtime
476476
func GoVersion() (int, int, int) {
477-
versionParts := strings.Split(runtime.Version(), ".")
478-
major, _ := strconv.Atoi(versionParts[0][2:])
479-
minor, _ := strconv.Atoi(versionParts[1])
480-
build, _ := strconv.Atoi(versionParts[2])
477+
return parseGoVersion(runtime.Version())
478+
}
479+
480+
// parseGoVersion parses Go version.
481+
// example:
482+
// - go1.19rc2
483+
// - go1.19beta2
484+
// - go1.19.4
485+
// - go1.19
486+
func parseGoVersion(version string) (int, int, int) {
487+
exp := regexp.MustCompile(`go(\d+).(\d+)(?:.(\d+))?.*`)
488+
parts := exp.FindStringSubmatch(version)
489+
if len(parts) <= 1 {
490+
return 0, 0, 0
491+
}
492+
493+
major, _ := strconv.Atoi(parts[1])
494+
minor, _ := strconv.Atoi(parts[2])
495+
build, _ := strconv.Atoi(parts[3])
496+
481497
return major, minor, build
482498
}

0 commit comments

Comments
 (0)