go - 如何在golang中比较两个版本号字符串

标签 go

我有两个字符串(它们实际上是版本号,可以是任何版本号)

a := "1.05.00.0156"  
b := "1.0.221.9289"

我想比较哪个更大。在golang中怎么做?

最佳答案

Hashicorp 提供了一个不错的解决方案 - https://github.com/hashicorp/go-version

import github.com/hashicorp/go-version
v1, err := version.NewVersion("1.2")
v2, err := version.NewVersion("1.5+metadata")
// Comparison example. There is also GreaterThan, Equal, and just
// a simple Compare that returns an int allowing easy >=, <=, etc.
if v1.LessThan(v2) {
    fmt.Printf("%s is less than %s", v1, v2)
}

关于go - 如何在golang中比较两个版本号字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18409373/

相关文章:

go - 将共享库添加到 AWS Lambda Go 二进制文件

go - jung-kurt/gofpdf TM商标符号未正确呈现

bash - Docker容器不断在终端注册Enter Key

linux - 使用 GoLang 自动化 `mysql_secure_installation`

logging - 如何将自定义(非 const)字符串信息添加到日志格式?

go - 在golang中 sleep 会阻止其他goroutine吗?

go - 如何在 Go 中进行类型转换

qt - Golang与Qt在paintEvent中的绘画问题

logging - 在 Golang 中将每个日志保存在单独的行中

google-app-engine - 如何使用 Go 从 Google App Engine 的模型中删除字段?