given a version string, return a number example: 1.2.3 -> 10203 how it works: 1. split the version string by "." 2. reverse the array 3. for each element, convert it to an integer 4. multiply the integer by 100^index 5. add the integer to the total 6. return the total
(v string)
| 91 | // 5. add the integer to the total |
| 92 | // 6. return the total |
| 93 | func FormatVersionStr(v string) int64 { |
| 94 | vs := strings.Split(v, ".") |
| 95 | if len(vs) <= 0 { |
| 96 | log.Panic("Version str error") |
| 97 | } |
| 98 | var vNum int64 |
| 99 | ReverseArr(vs) |
| 100 | for index, v := range vs { |
| 101 | num, err := strconv.ParseInt(v, 10, 64) |
| 102 | if err != nil { |
| 103 | log.Panic(err.Error()) |
| 104 | } |
| 105 | for i := 0; i < index; i++ { |
| 106 | num = num * 100 |
| 107 | } |
| 108 | vNum += num |
| 109 | } |
| 110 | return vNum |
| 111 | } |
| 112 | |
| 113 | func ReverseArr(s interface{}) { |
| 114 | sort.SliceStable(s, func(i, j int) bool { |
no test coverage detected