Skip to content

Instantly share code, notes, and snippets.

@tiborvass
Created March 1, 2019 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiborvass/05f99086c7fce95c941c9aafb6d1b83e to your computer and use it in GitHub Desktop.
Save tiborvass/05f99086c7fce95c941c9aafb6d1b83e to your computer and use it in GitHub Desktop.
versions
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