package main
import (
"fmt"
"strings"
)
var (
VersionBig = 1
VersionSmall = 2
VersionEqual = 0
)
func compareStrVer(verA, verB string) int {
verStrArrA := spliteStrByNet(verA)
verStrArrB := spliteStrByNet(verB)
lenStrA := len(verStrArrA)
lenStrB := len(verStrArrB)
if lenStrA != lenStrB {
panic("")
}
return compareArrStrVers(verStrArrA, verStrArrB)
}
func compareArrStrVers(verA, verB []string) int {
for index, _ := range verA {
littleResult := compareLittleVer(verA[index], verB[index])
if littleResult != VersionEqual {
return littleResult
}
}
return VersionEqual
}
func compareLittleVer(verA, verB string) int {
bytesA := []byte(verA)
bytesB := []byte(verB)
lenA := len(bytesA)
lenB := len(bytesB)
if lenA > lenB {
return VersionBig
}
if lenA < lenB {
return VersionSmall
}
return compareByBytes(bytesA, bytesB)
}
func compareByBytes(verA, verB []byte) int {
for index, _ := range verA {
if verA[index] > verB[index] {
return VersionBig
}
if verA[index] < verB[index] {
return VersionSmall
}
}
return VersionEqual
}
func spliteStrByNet(strV string) []string {
return strings.Split(strV, ".")
}
func main() {
versionA := "1.2.3.54"
versionB := "1.2.3.54"
fmt.Println(compareStrVer(versionA, versionB))
}
golang:比较版本号大小
最新推荐文章于 2024-08-17 22:16:33 发布