github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/helper_go_ver_test.go (about)

     1  package jzon
     2  
     3  import (
     4  	"log"
     5  	"runtime"
     6  	"strconv"
     7  	"strings"
     8  )
     9  
    10  var (
    11  	goVersion = newGoVersionInfo(runtime.Version())
    12  )
    13  
    14  func init() {
    15  	log.Println("the current go version is:", runtime.Version())
    16  }
    17  
    18  type goVersionInfo struct {
    19  	Major int
    20  	Minor int
    21  	Build int
    22  }
    23  
    24  func newGoVersionInfo(v string) (gv goVersionInfo) {
    25  	if !strings.HasPrefix(v, "go") {
    26  		return
    27  	}
    28  	arr := strings.Split(v[2:], ".")
    29  	if len(arr) != 3 {
    30  		return
    31  	}
    32  	major, err := strconv.Atoi(arr[0])
    33  	if err != nil {
    34  		return
    35  	}
    36  	minor, err := strconv.Atoi(arr[1])
    37  	if err != nil {
    38  		return
    39  	}
    40  	build, err := strconv.Atoi(arr[2])
    41  	if err != nil {
    42  		return
    43  	}
    44  	gv.Major = major
    45  	gv.Minor = minor
    46  	gv.Build = build
    47  	return
    48  }
    49  
    50  func (gv goVersionInfo) LessEqual(v string) bool {
    51  	other := newGoVersionInfo(v)
    52  	if gv.Major > other.Major {
    53  		return false
    54  	}
    55  	if gv.Major < other.Major {
    56  		return true
    57  	}
    58  	if gv.Minor > other.Minor {
    59  		return false
    60  	}
    61  	if gv.Minor < other.Minor {
    62  		return true
    63  	}
    64  	return gv.Build <= other.Build
    65  }