Comparing Versions In Go

If you’re using Go and working with Semantic Versioning (SemVer) there are occasions you want to compare version within a Go application. The standard library doesn’t have a package that can do this. Luckily, there are some community packages that can help. Here we’ll look at two such packages with differing feature sets.

github.com/hashicorp/go-version

Designed specifically for SemVer, this package from Hashicorp has a number of objects that work well together to provide a foundation for working with versions. It includes the ability to compare version, see if a version matches a set of constraints, and even order a set of versions.

Comparisons

To compare two versions a version.Version object is created for each version and then two are compared as greater than, less than, or equal. For example:

v1, err := version.NewVersion("1.0")
v2, err := version.NewVersion("1.1-beta1")
if v1.LessThan(v2) {
    fmt.Printf("%s is less than %s", v1, v2)
}

The other comparison functions are Equal and GreaterThan.

Constraints

Sometimes you’ll want to compare a version against a set of constraints. For example, you want to make sure a version is greater than 1.3, because a feature was added in that version, and less than version 2.0 where the API changed.

v1, err := version.NewVersion("1.3")

constraints, err := version.NewConstraint(">= 1.3, < 2.0")
if constraints.Check(v1) {
    fmt.Printf("%s satisfies the constraints %s", v1, constraints)
}

github.com/mcuadros/go-version

If you’re interested in working with versions as strings or dealing with versions that aren’t using SemVer this package maybe be more up your alley. It’s inspired by Composer and version handling in PHP.

Normalize Then Compare

Before you can compare version strings you need to normalize them. There’s a handy function to do this.

v := version.Normalize("8.3.12-b")
// v is 8.3.12.0-beta

Comparisons

There are two functions in this package for handling comparisons.

v := version.CompareSimple("1.1", "1.0")
// v is 1

If the first version is newer the response is 1, if the second version is newer the response is -1, and if they are the same the response is 0.

The version.Compare allows you to specify a 3rd parameter with the comparison string. For example,

version.Compare("2.0-dev", "1.0", "<")
version.Compare("1.0-alpha", "1.0", ">=")

The response from these are bool values.

Constraints

As with the package from Hashicorp you can set constraints and see if a version matches them.

c := version.NewConstrainGroupFromString(">1.0,<=2.0")
c.Match("1.5")

The response from this would be true as the version is within the constraints. The documentation provides numerous examples and lots of details on the strings you can compare.