func (v1 Version) Compare(v2 Version) int {
	if reflect.DeepEqual(v1, v2) {
		return 0
	}
	if v1.epoch > v2.epoch {
		return 1
	} else if v1.epoch < v2.epoch {
		return -1
	}
	ret := compare(v1.upstreamVersion, v2.upstreamVersion)
	if ret != 0 {
		return ret
	}
	return compare(v1.debianRevision, v2.debianRevision)
}
Compare returns an integer comparing two version according to deb-version.
The result will be 0 if v1==v2, -1 if v1 < v2, and +1 if v1 > v2.
package main
import (
	"errors"
	"fmt"
	"reflect"
	"regexp"
	"strconv"
	"strings"
	"unicode"
)
type defaultNumSlice []int
func (n defaultNumSlice) get(i int) int {
	if len(n) > i {
		return n[i]
	}
	return 0
}
type defaultStringSlice []string
func (s defaultStringSlice) get(i int) string {
	if len(s) > i {
		return s[i]
	}
	return ""
}
type Version struct {
	epoch           int
	upstreamVersion string
	debianRevision  string
}
var (
	digitRegexp    = regexp.MustCompile(`[0-9]+`)
	nonDigitRegexp = regexp.MustCompile(`[^0-9]+`)
)
func NewVersion(ver string) (version Version, err error) {
	ver = strings.TrimSpace(ver)
	splitted := strings.SplitN(ver, ":", 2)
	if len(splitted) == 1 {
		version.epoch = 0
		ver = splitted[0]
	} else {
		version.epoch, err = strconv.Atoi(splitted[0])
		if err != nil {
			return Version{}, fmt.Errorf("epoch parse error: %v", err)
		}
		if version.epoch < 0 {
			return Version{}, errors.New("epoch is negative")
		}
		ver = splitted[1]
	}
	index := strings.LastIndex(ver, "-")
	if index >= 0 {
		version.upstreamVersion = ver[:index]
		version.debianRevision = ver[index+1:]
	} else {
		version.upstreamVersion = ver
	}
	err = verifyUpstreamVersion(version.upstreamVersion)
	if err != nil {
		return Version{}, err
	}
	err = verifyDebianRevision(version.debianRevision)
	if err != nil {
		return Version{}, err
	}
	return version, nil
}
func verifyUpstreamVersion(str string) error {
	if len(str) == 0 {
		return errors.New("upstream_version is empty")
	}
	if !unicode.IsDigit(rune(str[0])) {
		return errors.New("upstream_version must start with digit")
	}
	allowedSymbols := ".-+~:_"
	for _, s := range str {
		if !unicode.IsDigit(s) && !unicode.IsLetter(s) && !strings.ContainsRune(allowedSymbols, s) {
			return errors.New("upstream_version includes invalid character")
		}
	}
	return nil
}
func verifyDebianRevision(str string) error {
	allowedSymbols := "+.~_"
	for _, s := range str {
		if !unicode.IsDigit(s) && !unicode.IsLetter(s) && !strings.ContainsRune(allowedSymbols, s) {
			return errors.New("debian_revision includes invalid character")
		}
	}
	return nil
}
func (v1 *Version) Equal(v2 Version) bool {
	return v1.Compare(v2) == 0
}
func (v1 *Version) GreaterThan(v2 Version) bool {
	return v1.Compare(v2) > 0
}
func (v1 Version) LessThan(v2 Version) bool {
	return v1.Compare(v2) < 0
}