Created
March 1, 2019 23:42
-
-
Save tiborvass/05f99086c7fce95c941c9aafb6d1b83e to your computer and use it in GitHub Desktop.
versions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"strconv" | |
"strings" | |
) | |
type version []int | |
func parseVersion(s string) (version, error) { | |
var v version | |
for _, s := range strings.Split(s, ".") { | |
i, err := strconv.Atoi(s) | |
if err != nil { | |
return nil, err | |
} | |
v = append(v, i) | |
} | |
return v, nil | |
} | |
func (v1 version) compare(v2 version) int { | |
if len(v2) > len(v1) { | |
z := make([]int, len(v2) - len(v1)) | |
v1 = append(v1, z...) | |
} else if len(v2) < len(v1) { | |
z := make([]int, len(v1) - len(v2)) | |
v2 = append(v2, z...) | |
} | |
for i := 0; i < len(v1); i++ { | |
if v1[i] != v2[i] { | |
if v1[i] < v2[i] { | |
return -1 | |
} | |
return 1 | |
} | |
} | |
return 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment