[Golang] Code snippets - Semver comparison
To make the project backward-compatible, a common util function for comparing the semantic version is indispensable. Here is a Golang solution implemented by leveraging go-semver package. package utils import "github.com/coreos/go-semver/semver" // CompareSemanticVersion compares two string written in semver format // return 0 indicates that the first version equals to the second version // return 1 indicates that the first version is greater than the second version // return -1 indicates that the first version is less than the second version func CompareSemanticVersion(current, last string) int { if current == last { return 0 } currentVersion := semver.New(current) lastVersion := semver.New(last) if currentVersion.LessThan(*lastVersion) { return -1 } return 1 } The unit test: ...