DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • High-Performance Java Serialization to Different Formats
  • Java Memory Management
  • Five Nonprofit & Charity APIs That Make Due Diligence Way Less Painful for Developers
  • Writing DTOs With Java8, Lombok, and Java14+

Trending

  • Prompt Injection Is Real, So I Built a Python Firewall for LLM Pipelines
  • Mastering Fluent Bit: Beginners' Guide for Contributing to Our CNCF Project Website
  • Advanced Error Handling and Retry Patterns in Enterprise REST Integrations
  • Give Your AI Assistant Long-Term Memory With perag
  1. DZone
  2. Coding
  3. JavaScript
  4. Comparing Versions in Go

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.

By 
Matt Farina user avatar
Matt Farina
·
Jul. 15, 15 · Opinion
Likes (1)
Comment
Save
Tweet
Share
3.3K Views

Join the DZone community and get the full member experience.

Join For Free

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.

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 versions, 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.

Comparison (grammar) Strings Standard library Object (computer science) application Composer (software) Documentation Foundation (framework) IT

Published at DZone with permission of Matt Farina. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • High-Performance Java Serialization to Different Formats
  • Java Memory Management
  • Five Nonprofit & Charity APIs That Make Due Diligence Way Less Painful for Developers
  • Writing DTOs With Java8, Lombok, and Java14+

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook