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
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
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

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • High-Performance Java Serialization to Different Formats
  • Challenges of Creating Digital Twins in the Transition to Industry 4.0
  • Java Memory Management
  • Five Ways to Improve the Understandability of your Software

Trending

  • How To Handle Technical Debt in Scrum
  • Continuous Integration vs. Continuous Deployment
  • Distributed Tracing Best Practices
  • Analyzing Stock Tick Data in SingleStoreDB Using LangChain and OpenAI's Whisper
  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.

Matt Farina user avatar by
Matt Farina
·
Jul. 15, 15 · Opinion
Like (1)
Save
Tweet
Share
2.30K 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
  • Challenges of Creating Digital Twins in the Transition to Industry 4.0
  • Java Memory Management
  • Five Ways to Improve the Understandability of your Software

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • 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: