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