Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two version number strings in golang

Tags:

I have two strings (they are actually version numbers and they could be any version numbers)

a := "1.05.00.0156"   b := "1.0.221.9289" 

I want to compare which one is bigger. How to do it in golang?

like image 469
JVK Avatar asked Aug 23 '13 18:08

JVK


People also ask

How can I compare two versions?

Open one of the two versions of the document that you want to compare. On the Tools menu, point to Track Changes, and then click Compare Documents. In the Original document list, select the original document. In the Revised document list, browse to the other version of the document, and then click OK.

How can I compare two Javascript versions?

The basic idea to make this comparison would be to use Array. split to get arrays of parts from the input strings and then compare pairs of parts from the two arrays; if the parts are not equal we know which version is smaller.


1 Answers

There is a nice solution from Hashicorp - https://github.com/hashicorp/go-version

import github.com/hashicorp/go-version v1, err := version.NewVersion("1.2") v2, err := version.NewVersion("1.5+metadata") // Comparison example. There is also GreaterThan, Equal, and just // a simple Compare that returns an int allowing easy >=, <=, etc. if v1.LessThan(v2) {     fmt.Printf("%s is less than %s", v1, v2) } 
like image 120
krab Avatar answered Oct 21 '22 14:10

krab