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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • High-Performance Java Serialization to Different Formats
  • Java Memory Management
  • Five Ways to Improve the Understandability of your Software
  • How to Use State Inside of an Effect Component With ngrx

Trending

  • Docker Model Runner: Streamlining AI Deployment for Developers
  • A Simple, Convenience Package for the Azure Cosmos DB Go SDK
  • Designing a Java Connector for Software Integrations
  • AWS to Azure Migration: A Cloudy Journey of Challenges and Triumphs
  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.0K 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, DZone MVB. 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 Ways to Improve the Understandability of your Software
  • How to Use State Inside of an Effect Component With ngrx

Partner Resources

×

Comments

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: