A minimal and fast Go library for working with Semantic Versioning (SemVer2.0.0).
- Accepts versions with or without the
vprefix (1.2.3/v1.2.3). - Supports shorthands:
MAJORandMAJOR.MINOR-> normalized tovMAJOR.0.0andvMAJOR.MINOR.0. Prerelease/build require fullMAJOR.MINOR.PATCH. - Canonical form:
vMAJOR.MINOR.PATCH[-PRERELEASE](build metadata stripped). Semverstruct withMajor,Minor,Patch,Prerelease,Build,Original, and rich helpers.- Correct comparison/sorting per SemVer (build is ignored). Stable tie-break by the original string.
- Mutators:
BumpPatch/Minor/Major,WithPre,WithBuild,StripPre,StripBuild,NextPrerelease, plusIsGreater/IsLower/IsEqual. - Performance:
Parse/Compare0 allocs/op (amd64, Go 1.18+). Rendering ~ 1 alloc.
PrereleaseandBuildare zero-copy slices ofOriginalright afterParse, after mutators they may be standalone strings.
Differences vs golang.org/x/mod/semver
- Optional leading
von input. - Shorthands
MAJOR,MAJOR.MINOR. - A convenient
Semvertype + methods (mutators, rendering). - Numeric components must fit into
uint64; otherwise the version is invalid.
go get github.com/woozymasta/semverpackage main
import (
"fmt"
"github.com/woozymasta/semver"
)
func must(v semver.Semver, ok bool) semver.Semver {
if !ok { panic("bad semver") }
return v
}
func main() {
v1 := must(semver.Parse("1.2.3-rc.1+meta"))
v2 := must(semver.Parse("v1.2.3"))
fmt.Println(v1.Canonical()) // v1.2.3-rc.1
fmt.Println(v2.Major) // 1
fmt.Println(v2.MajorStr()) // v1
// Compare (build ignored; release > prerelease for the same core)
switch v1.Compare(v2) {
case -1:
fmt.Println("v1 < v2")
case 0:
fmt.Println("v1 == v2")
case 1:
fmt.Println("v1 > v2")
}
}versions := []semver.Semver{
must(semver.Parse("1.2.3")),
must(semver.Parse("1.2.3-rc.1")),
must(semver.Parse("2.0.0")),
}
semver.List(versions).Sort()v := must(semver.Parse("1.2.3-rc.1+build.5"))
p, _ := v.BumpPatch() // v1.2.4
m, _ := v.BumpMinor() // v1.3.0
M, _ := v.BumpMajor() // v2.0.0
fmt.Println(p.Canonical(), m.Canonical(), M.Canonical())
// WithPre / WithBuild (validated per SemVer)
x := must(semver.Parse("1"))
x1, _ := x.WithPre("alpha.1") // v1.0.0-alpha.1
x2, _ := x1.WithBuild("meta.5") // v1.0.0-alpha.1+meta.5 (in StringV)
fmt.Println(x1.Canonical(), x2.StringV())
// NextPrerelease: bumps last numeric id, or appends ".1"
// empty base defaults to "rc"
z := must(semver.Parse("1.2.3-rc.9"))
z1, _ := z.NextPrerelease("rc") // v1.2.3-rc.10
z2 := must(semver.Parse("1.2.3"))
z2, _ = z2.NextPrerelease("") // v1.2.3-rc.1
fmt.Println(z1.Canonical(), z2.Canonical())
// Strip
r1, _ := v.StripPre() // canonical: v1.2.3; StringV can include +build if present
r2, _ := v.StripBuild() // canonical: v1.2.3-rc.1
_ = r1
_ = r2
// Rendering with/without original 'v' preservation
u := must(semver.Parse("V1.2.3-rc.1+meta"))
fmt.Println(u.StringV()) // "v1.2.3-rc.1+meta" (force lowercase 'v')
fmt.Println(u.String()) // "V1.2.3-rc.1+meta" (preserve original prefix)
// Canonical always uses 'v' and drops build:
fmt.Println(u.Canonical()) // "v1.2.3-rc.1"- Comparison: strict SemVer; build metadata does not affect ordering.
- Canonical: always starts with
v, build metadata removed. - Shorthands:
MAJOR,MAJOR.MINORaccepted (pragmatic deviation). - Numbers: must fit into
uint64; too large → invalid. - Go: tested with Go 1.18+.
Parse(s)accepts optionalv/VandMAJOR/MAJOR.MINORshorthands.ParseStrict(s)requiresMAJOR.MINOR.PATCHand rejectsv/V.New(major, minor, patch, pre, build)builds a complete, internally consistentSemverDTO.Compare,IsGreater,IsLower,IsEqualcompare precedence; build metadata does not affect it.List(versions).Sort()sorts by SemVer precedence with anOriginaltie-breaker.
Rendering:
String()preserves the inputv/Vstyle and includes build metadata.SemVer()uses no prefix and includes build metadata.StringV()uses lowercasevand includes build metadata.Canonical()uses lowercasevand removes build metadata.MajorStr(),MajorMinorStr(),ReleaseStr()use lowercasevand return progressively more of the release core.
State and metadata:
- Validity:
IsValid(),IsStrict(),IsRelease(),IsPlain(). - Presence:
HasV(),HasMajor(),HasMinor(),HasPatch(),HasPre(),HasBuild(). - Mutators returning a new value:
BumpPatch(),BumpMinor(),BumpMajor(),WithPre(),WithBuild(),StripPre(),StripBuild(),NextPrerelease(). - Serialization:
encoding.TextMarshaler,encoding.TextUnmarshaler,json.Marshaler, andjson.Unmarshaler.
For Calendar Versioning, use the companion WoozyMasta/calver library.