Semantic Versioning (semver) library written in golang
Semantic Versioning (semver) library written in golang
semver is a Semantic Versioning library written in golang. It fully covers spec version 2.0.0.
Old v1-v3 versions exist in the root of the repository for compatiblity reasons and will only receive bug fixes.
The current stable version is v4 and is fully go-mod compatible.
$ go get github.com/blang/semver/v4
# Or use fixed versions
$ go get github.com/blang/semver/[email protected]Note: Always vendor your dependencies or fix on a specific version tag.
import github.com/blang/semver/v4
v1, err := semver.Make("1.0.0-beta")
v2, err := semver.Make("2.0.0-beta")
v1.Compare(v2)Also check the GoDocs.
>=1.0.0 =3.0.0 !3.0.1-beta.1>=1.x, 1.0.0 Greater than 1.0.0>=1.0.0 Greater than or equal to 1.0.01.0.0, =1.0.0, ==1.0.0 Equal to 1.0.0!1.0.0, !=1.0.0 Not equal to 1.0.0. Excludes version 1.0.0.Note that spaces between the operator and the version will be gracefully tolerated.
A Range can link multiple Ranges separated by space:
Ranges can be linked by logical AND:
>1.0.0 1.0.0 =3.0.0 would match 1.x.x and 3.x.x but not 2.x.xAND has a higher precedence than OR. It's not possible to use brackets.
Ranges can be combined by both AND and OR
>1.0.0 3.0.0 !4.2.1 would match 1.2.3, 1.9.9, 3.1.1, but not 4.2.1, 2.1.1Range usage:
v, err := semver.Parse("1.2.3")
expectedRange, err := semver.ParseRange(">1.0.0 =3.0.0")
if expectedRange(v) {
//valid
}
Have a look at full examples in v4/examples/main.go
…BenchmarkParseSimple-4 5000000 390 ns/op 48 B/op 1 allocs/op
BenchmarkParseComplex-4 1000000 1813 ns/op 256 B/op 7 allocs/op
BenchmarkParseAverage-4 1000000 1171 ns/op 163 B/op 4 allocs/op
BenchmarkStringSimple-4 20000000 119 ns/op 16 B/op 1 allocs/op
BenchmarkStringLarger-4 10000000 206 ns/op 32 B/op 2 allocs/op
BenchmarkStringComplex-4 5000000 324 ns/op 80 B/op 3 allocs/op
BenchmarkStringAverage-4 5000000 273 ns/op 53 B/op 2 allocs/op
BenchmarkValidateSimple-4 200000000 9.33 ns/op 0 B/op 0 allocs/op
BenchmarkValidateComplex-4 3000000 469 ns/op 0 B/op 0 allocs/op
BenchmarkValidateAverage-4 5000000 256 ns/op 0 B/op 0 allocs/op
BenchmarkCompareSimple-4 100000000 11.8 ns/op 0 B/op 0 allocs/op
BenchmarkCompareComplex-4 50000000 30.8 ns/op 0 B/op 0 allocs/op
BenchmarkCompareAverage-4 30000000 41.5 ns/op 0 B/op 0 allocs/op
BenchmarkSort-4 3000000 419 ns/op 256 B/op 2 allocs/op
BenchmarkRangeParseSimple-4 2000000 850 ns/op 192 B/op 5 allocs/op
BenchmarkRangeParseAverage-4 1000000 1677 ns/op 400 B/op 10 allocs/op
BenchmarkRangeParseComplex-4 300000 5214 ns/op 1440 B/op 30 allocs/op
BenchmarkRangeMatchSimple-4 50000000 25.6 ns/op 0 B/op 0 allocs/op
BenchmarkRangeMatchAverage-4 30000000 56.4 ns/op 0 B/op 0 allocs/op
BenchmarkRangeMatchComplex-4 10000000 153 ns/op 0 B/op 0 allocs/opSee benchmark cases at semver_test.go
I simply couldn't find any lib supporting the full spec. Others were just wrong or used reflection and regex which i don't like.
Feel free to make a pull request. For bigger changes create a issue first to discuss about it.
See LICENSE file.
No open issues yet, or sync has not completed.