如何比较golang中的两个版本号字符串

时间:2013-08-23 18:20:12

标签: go

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

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

我想比较哪一个更大。如何在golang中做到这一点?

11 个答案:

答案 0 :(得分:14)

前段时间我创建了一个版本比较库:https://github.com/mcuadros/go-version

version.CompareSimple("1.05.00.0156", "1.0.221.9289")
//Returns: 1

享受它!

答案 1 :(得分:7)

这是一般解决方案。

package main

import "fmt"

func VersionOrdinal(version string) string {
    // ISO/IEC 14651:2011
    const maxByte = 1<<8 - 1
    vo := make([]byte, 0, len(version)+8)
    j := -1
    for i := 0; i < len(version); i++ {
        b := version[i]
        if '0' > b || b > '9' {
            vo = append(vo, b)
            j = -1
            continue
        }
        if j == -1 {
            vo = append(vo, 0x00)
            j = len(vo) - 1
        }
        if vo[j] == 1 && vo[j+1] == '0' {
            vo[j+1] = b
            continue
        }
        if vo[j]+1 > maxByte {
            panic("VersionOrdinal: invalid version")
        }
        vo = append(vo, b)
        vo[j]++
    }
    return string(vo)
}

func main() {
    versions := []struct{ a, b string }{
        {"1.05.00.0156", "1.0.221.9289"},
        // Go versions
        {"1", "1.0.1"},
        {"1.0.1", "1.0.2"},
        {"1.0.2", "1.0.3"},
        {"1.0.3", "1.1"},
        {"1.1", "1.1.1"},
        {"1.1.1", "1.1.2"},
        {"1.1.2", "1.2"},
    }
    for _, version := range versions {
        a, b := VersionOrdinal(version.a), VersionOrdinal(version.b)
        switch {
        case a > b:
            fmt.Println(version.a, ">", version.b)
        case a < b:
            fmt.Println(version.a, "<", version.b)
        case a == b:
            fmt.Println(version.a, "=", version.b)
        }
    }
}

输出:

1.05.00.0156 > 1.0.221.9289
1 < 1.0.1
1.0.1 < 1.0.2
1.0.2 < 1.0.3
1.0.3 < 1.1
1.1 < 1.1.1
1.1.1 < 1.1.2
1.1.2 < 1.2

答案 2 :(得分:7)

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)
}

答案 3 :(得分:2)

根据Jeremy Wall的回答:

  func compareVer(a, b string) (ret int) {
            as := strings.Split(a, ".")
            bs := strings.Split(b, ".")
            loopMax := len(bs)
            if len(as) > len(bs) {
                    loopMax = len(as)
            }
            for i := 0; i < loopMax; i++ { 
                    var x, y string
                    if len(as) > i {
                            x = as[i]
                    }
                    if len(bs) > i {
                            y = bs[i]
                    }
                    xi,_ := strconv.Atoi(x)
                    yi,_ := strconv.Atoi(y)
                    if xi > yi {
                            ret = -1
                    } else if xi < yi {
                            ret = 1
                    }
                    if ret != 0 {
                            break
                    }
            }       
            return 
    }

http://play.golang.org/p/AetJqvFc3B

答案 4 :(得分:2)

go-semver是Go的semantic versioning库。它使您可以解析和比较两个语义版本字符串。

示例:

vA := semver.New("1.2.3")
vB := semver.New("3.2.1")

fmt.Printf("%s < %s == %t\n", vA, vB, vA.LessThan(*vB))

输出:

  

1.2.3 <3.2.1 ==真

答案 5 :(得分:1)

这取决于你的意思更大。

一种天真的方法是:

package main

import "fmt"
import "strings"

func main() {
    a := strings.Split("1.05.00.0156", ".")
    b := strings.Split("1.0.221.9289", ".")
    for i, s := range a {
        var ai, bi int
        fmt.Sscanf(s, "%d", &ai)
        fmt.Sscanf(b[i], "%d", &bi)
        if ai > bi {
            fmt.Printf("%v is bigger than %v\n", a, b)
            break
        }
        if bi > ai {
            fmt.Printf("%v is bigger than %v\n", b, a)
            break
        }
    }
}

http://play.golang.org/p/j0MtFcn44Z

答案 6 :(得分:1)

努力清晰简洁:

func intVer(v string) (int64, error) {
    sections := strings.Split(v, ".")
    intVerSection := func(v string, n int) string {
        if n < len(sections) {
            return fmt.Sprintf("%04s", sections[n])
        } else {
            return "0000"
        }
    }
    s := ""
    for i := 0; i < 4; i++ {
        s += intVerSection(v, i)
    }
    return strconv.ParseInt(s, 10, 64)
}

func main() {
    a := "3.045.98.0832"
    b := "087.2345"
    va, _ := intVer(a)
    vb, _ := intVer(b)
    fmt.Println(va<vb)
}

比较版本意味着解析所以我认为这两个步骤应该是分开的,以使其健壮。

答案 7 :(得分:0)

import (
    "fmt"
    "strconv"
    "strings"
)


func main() {
    j := ll("1.05.00.0156"  ,"1.0.221.9289")
   fmt.Println(j)
}


func ll(a,b string) int {
    var length ,r,l int = 0,0,0
    v1 := strings.Split(a,".")
    v2 := strings.Split(b,".")
    len1, len2 := len(v1), len(v2)

    length = len2
    if len1 > len2 {
       length = len1
    }

    for i:= 0;i<length;i++ {
        if i < len1 && i < len2 {
            if v1[i] == v2[i] {
                continue
            }
        }
        r = 0
        if i < len1 {
            if number, err := strconv.Atoi(v1[i]); err == nil {
                r = number
            }
        }

        l = 0
        if i < len2 {
            if number, err := strconv.Atoi(v2[i]); err == nil {
                l = number
            }
        }

        if r < l {
            return -1
        }else if r> l {
            return 1
        }
    }

    return 0
}

答案 8 :(得分:0)

以下是一些用于版本比较的库:

  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

我使用了 blang / semver 。 例如: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

答案 9 :(得分:0)

如果你能保证版本字符串具有相同的格式(即 SemVer),你可以转换为 int 并比较 int。这是对 SemVer 的切片进行排序的实现:

    versions := []string{"1.0.10", "1.0.6", "1.0.9"}
    sort.Slice(versions[:], func(i, j int) bool {
        as := strings.Split(versions[i], ".")
        bs := strings.Split(versions[j], ".")
        if len(as) != len(bs) || len(as) != 3 {
            return versions[i] < versions[j]
        }

        ais := make([]int, len(as))
        bis := make([]int, len(bs))
        for i := range as {
            ais[i], _ = strconv.Atoi(as[i])
            bis[i], _ = strconv.Atoi(bs[i])
        }

        //X.Y.Z

        // If X and Y are the same, compare Z
        if ais[0] == bis[0] && ais[1] == bis[1] {
            return ais[2] < bis[2]
        }

        // If X is same, compare Y
        if ais[0] == bis[0] {
            return ais[1] < bis[1]
        }

        // Compare X
        return ais[0] < bis[0]
    })
    fmt.Println(versions)

答案 10 :(得分:-4)

将“1.05.00.0156”转换为“0001”+“0005”+“0000”+“0156”,然后转换为int64。

将“1.0.221.9289”转换为“0001”+“0000”+“0221”+“9289”,然后转换为int64。

比较两个int64值。

Go playground

上试试