Golang implementing PHP's Version_compare function

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

PHP Version_compare is a version number string that compares two "php normalization".

mixed version_compare ( string $version1 , string $version2 [, string $operator ] )

Use Golang to implement some of the commonly used features.

Package Utilimport "strings"//This function compares whether two version numbers are equal, is greater than or less than the relationship//return value: 0 means V1 is equal to V2, 1 means V1 is greater than v2;2 means V1 is less than V2func Compare (v1, v2 string) int {//replace some common version symbols Replacemap: = map[string]string{"V": "", "V": "", "-": ".",}//keywords: = {"Alpha,bet A,rc,p "} for K, V: = range Replacemap {if strings. Contains (v1, k) {strings. Replace (v1, K, V,-1)} if strings. Contains (v2, k) {strings. Replace (v2, K, V,-1)}} ver1: = Strings.    Split (v1, ".") Ver2: = Strings.    Split (v2, ".") Find out which V1 and V2 are the shortest var shorter int if Len (ver1) > Len (ver2) {shorter = Len (ver2)} else {shorter = Len (ver1)}//Loop compare for I: = 0; i < shorter;                    i++ {if ver1[i] = = Ver2[i] {if shorter-1 = = i {if len (ver1) = = Len (ver2) { Return 0} else {//@todo check for keywords if len (ver1) &G T   Len (ver2) {return 1                 } else {return 2}}} else If ver1[i] > Ver2[i] {return 1} else {return 2}} Return-1}func Version            Compare (v1, v2, operator string) bool {com: = Compare (v1,v2) switch operator {case "= =": if com = = 0 {        return true} case "<": if com = = 2 {return true} case ">": if com = = 1 {return true} case "<=": if com = = 0 | | com = = 2 {return true} case ">=": if com = = 0 | | com = = 1{return True}} return False}

Test Unit:

package utilimport (    "testing")var (    v1 = "2.0.1"    v1_1 = "2.0.1.1"    v2 = "2.1.1")// 该函数比较两个版本号是否相等,是否大于或小于的关系// 返回值:0表示v1与v2相等;1表示v1大于v2;2表示v1小于v2func TestCompare(t *testing.T) {    if 0 != Compare(v1,v1) {        t.Errorf("v1 %s == v1 %s ",v1,v1)    }    if 1 != Compare(v2,v1) {        t.Errorf("v2 %s > v1 %s",v2,v1)    }    if 2 != Compare(v1,v2) {        t.Errorf("v1 %s < v2 %s",v1,v2)    }}func TestVersionCompare(t *testing.T)  {    if !VersionCompare(v1,v1_1,"<") {        t.Errorf("v1 %s < v1_1 %s ",v1,v1_1)    }    if !VersionCompare(v2,v1_1,">") {        t.Errorf("v2 %s < v1_1 %s ",v2,v1_1)    }    if !VersionCompare(v2,v1,">=") {        t.Errorf("v2 %s >= v1 %s ",v2,v1)    }    if !VersionCompare(v1,v2,"<=") {        t.Errorf("v1 %s >= v2 %s ",v1,v2)    }    if !VersionCompare(v1,v1,"==") {        t.Errorf("v1 %s == v1 %s ",v1,v1)    }}

The code simply implements the "<,>,<=,>=,==".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.