Ben Chuanlong Du's Blog

It is never too late to learn.

Parse Version Semantics in Golang

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

In [1]:
import (
    "github.com/mcuadros/go-version"
)
In [3]:
version.Normalize("10.4.13-b")
Out[3]:
10.4.13.0-beta
In [4]:
version.CompareSimple("1.2", "1.0.1")
Out[4]:
1
In [5]:
version.CompareSimple("1.0rc1", "1.0")
Out[5]:
-1
In [6]:
version.Compare("1.0-dev", "1.0", "<")
Out[6]:
true
In [7]:
version.Compare("1.0rc1", "1.0", ">=")
Out[7]:
false
In [8]:
version.Compare("2.3.4", "v3.1.2", "<")
Out[8]:
true

Version Constraints

In [ ]:
c := version.NewConstrainGroupFromString(">2.0,<=3.0")
c.Match("2.5.0beta")
//Returns: true

c := version.NewConstrainGroupFromString("~1.2.3")
c.Match("1.2.3.5")
//Returns: true

Sort Versions

In [12]:
arr := []string{"1.10-dev", "1.0rc1", "1.0", "1.0-dev"}
arr
Out[12]:
[1.10-dev 1.0rc1 1.0 1.0-dev]
In [13]:
version.Sort(arr)
arr
Out[13]:
[1.0-dev 1.0rc1 1.0 1.10-dev]
In [ ]:
 

Comments