From 1c9c71757982aa4701bf7d12d8080cd514f6a524 Mon Sep 17 00:00:00 2001 From: 4566704 <4566704@qq.com> Date: Sun, 20 Sep 2026 12:20:51 +0800 Subject: [PATCH] =?UTF-8?q?feat(verx):=20=E8=87=AA=20go-hua/common=20?= =?UTF-8?q?=E8=BF=81=E5=85=A5=E7=89=88=E6=9C=AC=E5=8F=B7=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E5=B9=B6=E7=8B=AC=E7=AB=8B=E6=88=90=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 点分版本号解析/还原/比较,CompareVersion 依赖 numx.Min - 附单元测试、examples/verx 例程与包 README --- examples/verx/main.go | 19 +++++++ verx/README.md | 26 ++++++++++ verx/verx.go | 117 ++++++++++++++++++++++++++++++++++++++++++ verx/verx_test.go | 30 +++++++++++ 4 files changed, 192 insertions(+) create mode 100644 examples/verx/main.go create mode 100644 verx/README.md create mode 100644 verx/verx.go create mode 100644 verx/verx_test.go diff --git a/examples/verx/main.go b/examples/verx/main.go new file mode 100644 index 0000000..9e9374a --- /dev/null +++ b/examples/verx/main.go @@ -0,0 +1,19 @@ +// verx 包示例:版本号解析与比较 +package main + +import ( + "fmt" + + "git.zeroonesoft.cn/golib/zogo/verx" +) + +func main() { + // 结构化版本号:解析 / 还原 / 比较 + ver := verx.StringToVersionInfo("1.2.3") + fmt.Println("解析结果:", verx.VersionToString(ver)) + fmt.Println("1.2.3 vs 2.0.0.0:", verx.VersionComparison(ver, verx.StringToVersionInfo("2.0.0.0"))) + + // 点分字符串直接比较:1 大于 / -1 小于 / 0 等于 + fmt.Println("CompareVersion(1.2.0, 1.10.0):", verx.CompareVersion("1.2.0", "1.10.0")) + fmt.Println("CompareVersion(2.0, 1.9.9):", verx.CompareVersion("2.0", "1.9.9")) +} diff --git a/verx/README.md b/verx/README.md new file mode 100644 index 0000000..420e741 --- /dev/null +++ b/verx/README.md @@ -0,0 +1,26 @@ +# verx + +版本号工具:点分版本号与结构互转、版本比较。 + +> 迁移自 go-hua/common(原 version.go,Max/Min 归入 numx)。 + +## 用法 + +```go +import "git.zeroonesoft.cn/golib/zogo/verx" + +// 结构化版本:解析 / 还原 / 比较 +ver := verx.StringToVersionInfo("1.2.3") // {1,2,3,0} +s := verx.VersionToString(ver) // "1.2.3.0" +verx.VersionComparison(ver, verx.StringToVersionInfo("2.0.0.0")) // -1 + +// 点分字符串直接比较:1 大于 / -1 小于 / 0 等于 +verx.CompareVersion("1.2.0", "1.10.0") // -1 +``` + +完整可运行例程:[examples/verx/main.go](../examples/verx/main.go) + +## 注意 + +- `VersionInfo` 每段为 int64,但与 2 字节互转的历史约定仍在:超过 65535 会溢出截断。 +- `CompareVersion` 按段逐段数字比较,段数不同时缺段按 0 处理("2.0" > "1.9.9")。 diff --git a/verx/verx.go b/verx/verx.go new file mode 100644 index 0000000..08d8bc3 --- /dev/null +++ b/verx/verx.go @@ -0,0 +1,117 @@ +// Package verx 版本号工具:点分版本号与结构互转、版本比较。 +// VersionInfo 每段 2 字节(int64 存储,超出会溢出截断)。 +package verx + +import ( + "fmt" + "strconv" + "strings" + + "git.zeroonesoft.cn/golib/zogo/numx" +) + +// VersionInfo 版本号结构(每段 2 字节,与点分字符串互转) +type VersionInfo struct { + VersionMajor int64 `json:"versionMajor"` + VersionMinor int64 `json:"versionMinor"` + VersionPatch int64 `json:"versionPatch"` + VersionBuild int64 `json:"versionBuild"` +} + +// StringToVersionInfo 解析点分版本号字符串(如 1.2.3),每段最多 2 字节,超出会溢出截断 +func StringToVersionInfo(str string) VersionInfo { + ver := VersionInfo{0, 0, 0, 0} + arr := strings.Split(str, ".") + if len(arr) > 0 { + ver.VersionMajor, _ = strconv.ParseInt(arr[0], 10, 64) + } + if len(arr) > 1 { + ver.VersionMinor, _ = strconv.ParseInt(arr[1], 10, 64) + } + if len(arr) > 2 { + ver.VersionPatch, _ = strconv.ParseInt(arr[2], 10, 64) + } + if len(arr) > 3 { + ver.VersionBuild, _ = strconv.ParseInt(arr[3], 10, 64) + } + + return ver +} + +// VersionComparison 版本比较 1.大于 -1.小于 0.等于 +func VersionComparison(ver1 VersionInfo, ver2 VersionInfo) int { + + if ver1.VersionMajor > ver2.VersionMajor { + return 1 + } else if ver1.VersionMajor < ver2.VersionMajor { + return -1 + } + + if ver1.VersionMinor > ver2.VersionMinor { + return 1 + } else if ver1.VersionMinor < ver2.VersionMinor { + return -1 + } + + if ver1.VersionPatch > ver2.VersionPatch { + return 1 + } else if ver1.VersionPatch < ver2.VersionPatch { + return -1 + } + + if ver1.VersionBuild > ver2.VersionBuild { + return 1 + } else if ver1.VersionBuild < ver2.VersionBuild { + return -1 + } + + return 0 +} + +// VersionToString 将版本结构转为点分字符串 +func VersionToString(ver VersionInfo) string { + str := fmt.Sprintf("%d.%d.%d.%d", ver.VersionMajor, ver.VersionMinor, ver.VersionPatch, ver.VersionBuild) + return str +} + +// CompareVersion 比较两个版本号 version1 和 version2,如果 version1 > version2 返回 1,如果 version1 < version2 返回 -1, 除此之外返回 0。 +func CompareVersion(version1 string, version2 string) int { + var arr1 = strings.Split(version1, ".") + var arr2 = strings.Split(version2, ".") + var length1 = len(arr1) + var length2 = len(arr2) + var length = numx.Min(length1, length2) + v1 := 0 + v2 := 0 + for i := 0; i < length; i++ { + + if i < length1 { + v1, _ = strconv.Atoi(arr1[i]) + } else { + v1 = 0 + } + + if i < length2 { + v2, _ = strconv.Atoi(arr2[i]) + } else { + v2 = 0 + } + + if v1 != v2 { + if v1 > v2 { + return 1 + } else if v1 < v2 { + return -1 + } + } + + } + + if v1 > v2 { + return 1 + } else if v1 < v2 { + return -1 + } + return 0 + +} diff --git a/verx/verx_test.go b/verx/verx_test.go new file mode 100644 index 0000000..14ee521 --- /dev/null +++ b/verx/verx_test.go @@ -0,0 +1,30 @@ +package verx + +import "testing" + +func TestVersionFunctions(t *testing.T) { + ver := StringToVersionInfo("1.2.3") + if ver.VersionMajor != 1 || ver.VersionMinor != 2 || ver.VersionPatch != 3 || ver.VersionBuild != 0 { + t.Errorf("StringToVersionInfo = %+v", ver) + } + if got := VersionToString(ver); got != "1.2.3.0" { + t.Errorf("VersionToString = %q, want 1.2.3.0", got) + } + + if got := VersionComparison(StringToVersionInfo("2.0.0.0"), StringToVersionInfo("1.9.9.9")); got != 1 { + t.Errorf("VersionComparison 2.0.0.0 vs 1.9.9.9 = %d, want 1", got) + } + if got := VersionComparison(StringToVersionInfo("1.0.0.0"), StringToVersionInfo("1.0.0.0")); got != 0 { + t.Errorf("VersionComparison 相同版本 = %d, want 0", got) + } + + if got := CompareVersion("1.2.0", "1.10.0"); got != -1 { + t.Errorf("CompareVersion(1.2.0, 1.10.0) = %d, want -1", got) + } + if got := CompareVersion("2.0", "1.9.9"); got != 1 { + t.Errorf("CompareVersion(2.0, 1.9.9) = %d, want 1", got) + } + if got := CompareVersion("1.0.0", "1.0.0"); got != 0 { + t.Errorf("CompareVersion 相同 = %d, want 0", got) + } +}