Over a million developers have joined DZone.

Comparing Versions in Go

DZone's Guide to

Comparing Versions in Go

If you're using Go and working with Semantic Versioning you'll sometimes need to compare versions. Here we take a look at two community packages that do this.

· DevOps Zone ·
Free Resource

Get the fastest log management and analysis with Graylog open source or enterprise edition free up to 5GB per day

If you're using Go and working with Semantic Versioning (SemVer) there are occasions you want to compare versions 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.


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 versions, see if a version matches a set of constraints, and even order a set of versions.


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.


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)


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


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.


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

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

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.

Get the fastest log management and analysis with Graylog open source or enterprise edition free up to 5GB per day

devops ,go ,version control

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}