version.go 778 B

1234567891011121314151617181920212223242526272829303132333435
  1. package source
  2. import (
  3. "runtime"
  4. "strconv"
  5. "strings"
  6. )
  7. // GoVersionLessThan returns true if runtime.Version() is semantically less than
  8. // version major.minor. Returns false if a release version can not be parsed from
  9. // runtime.Version().
  10. func GoVersionLessThan(major, minor int64) bool {
  11. version := runtime.Version()
  12. // not a release version
  13. if !strings.HasPrefix(version, "go") {
  14. return false
  15. }
  16. version = strings.TrimPrefix(version, "go")
  17. parts := strings.Split(version, ".")
  18. if len(parts) < 2 {
  19. return false
  20. }
  21. rMajor, err := strconv.ParseInt(parts[0], 10, 32)
  22. if err != nil {
  23. return false
  24. }
  25. if rMajor != major {
  26. return rMajor < major
  27. }
  28. rMinor, err := strconv.ParseInt(parts[1], 10, 32)
  29. if err != nil {
  30. return false
  31. }
  32. return rMinor < minor
  33. }