How to compare two version number strings in golang

前端 未结 10 740
别跟我提以往
别跟我提以往 2020-12-30 02:06

I have two strings (they are actually version numbers and they could be any version numbers)

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

10条回答
  •  失恋的感觉
    2020-12-30 02:38

    There is a nice solution from 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)
    }
    

提交回复
热议问题