github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/goenv/version.go (about) 1 package goenv 2 3 import ( 4 "errors" 5 "fmt" 6 "io" 7 "strings" 8 ) 9 10 // Version of TinyGo. 11 // Update this value before release of new version of software. 12 const version = "0.32.0-dev" 13 14 var ( 15 // This variable is set at build time using -ldflags parameters. 16 // See: https://stackoverflow.com/a/11355611 17 GitSha1 string 18 ) 19 20 // Return TinyGo version, either in the form 0.30.0 or as a development version 21 // (like 0.30.0-dev-abcd012). 22 func Version() string { 23 v := version 24 if strings.HasSuffix(version, "-dev") && GitSha1 != "" { 25 v += "-" + GitSha1 26 } 27 return v 28 } 29 30 // GetGorootVersion returns the major and minor version for a given GOROOT path. 31 // If the goroot cannot be determined, (0, 0) is returned. 32 func GetGorootVersion() (major, minor int, err error) { 33 s, err := GorootVersionString() 34 if err != nil { 35 return 0, 0, err 36 } 37 38 if s == "" || s[:2] != "go" { 39 return 0, 0, errors.New("could not parse Go version: version does not start with 'go' prefix") 40 } 41 42 parts := strings.Split(s[2:], ".") 43 if len(parts) < 2 { 44 return 0, 0, errors.New("could not parse Go version: version has less than two parts") 45 } 46 47 // Ignore the errors, we don't really handle errors here anyway. 48 var trailing string 49 n, err := fmt.Sscanf(s, "go%d.%d%s", &major, &minor, &trailing) 50 if n == 2 && err == io.EOF { 51 // Means there were no trailing characters (i.e., not an alpha/beta) 52 err = nil 53 } 54 if err != nil { 55 return 0, 0, fmt.Errorf("failed to parse version: %s", err) 56 } 57 return 58 } 59 60 // GorootVersionString returns the version string as reported by the Go 61 // toolchain. It is usually of the form `go1.x.y` but can have some variations 62 // (for beta releases, for example). 63 func GorootVersionString() (string, error) { 64 err := readGoEnvVars() 65 return goEnvVars.GOVERSION, err 66 }