How to compare two version number strings in golang

前端 未结 10 739
别跟我提以往
别跟我提以往 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:23

    Here are some of the libraries for version comparison:

    1. https://github.com/blang/semver
    2. https://github.com/Masterminds/semver
    3. https://github.com/hashicorp/go-version
    4. https://github.com/mcuadros/go-version

    I have used blang/semver. Eg: https://play.golang.org/p/1zZvEjLSOAr

     import github.com/blang/semver/v4
     
      v1, err := semver.Make("1.0.0-beta")
      v2, err := semver.Make("2.0.0-beta")
     
      // Options availabe
      v1.Compare(v2)  // Compare
      v1.LT(v2)       // LessThan
      v1.GT(v2)       // GreaterThan

提交回复
热议问题