Skip to content
WoozyMastaPublic

About

Fast, dependency-free Go library for parsing, validating, rendering, and comparing Semantic Versioning (SemVer 2.0.0) values with explicit formats

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

36 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

semver

A minimal and fast Go library for working with Semantic Versioning (SemVer2.0.0).

Features

  • Accepts versions with or without the v prefix (1.2.3 / v1.2.3).
  • Supports shorthands: MAJOR and MAJOR.MINOR -> normalized to vMAJOR.0.0 and vMAJOR.MINOR.0. Prerelease/build require full MAJOR.MINOR.PATCH.
  • Canonical form: vMAJOR.MINOR.PATCH[-PRERELEASE] (build metadata stripped).
  • Semver struct with Major, 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, plus IsGreater/IsLower/IsEqual.
  • Performance: Parse/Compare 0 allocs/op (amd64, Go 1.18+). Rendering ~ 1 alloc.

Prerelease and Build are zero-copy slices of Original right after Parse, after mutators they may be standalone strings.

  • Optional leading v on input.
  • Shorthands MAJOR, MAJOR.MINOR.
  • A convenient Semver type + methods (mutators, rendering).
  • Numeric components must fit into uint64; otherwise the version is invalid.

Installation

go get github.com/woozymasta/semver

Quick Start

package 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")
  }
}

Sorting

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()

Mutators & Rendering

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"

Compatibility

  • Comparison: strict SemVer; build metadata does not affect ordering.
  • Canonical: always starts with v, build metadata removed.
  • Shorthands: MAJOR, MAJOR.MINOR accepted (pragmatic deviation).
  • Numbers: must fit into uint64; too large → invalid.
  • Go: tested with Go 1.18+.

API Cheatsheet

  • Parse(s) accepts optional v/V and MAJOR / MAJOR.MINOR shorthands.
  • ParseStrict(s) requires MAJOR.MINOR.PATCH and rejects v/V.
  • New(major, minor, patch, pre, build) builds a complete, internally consistent Semver DTO.
  • Compare, IsGreater, IsLower, IsEqual compare precedence; build metadata does not affect it.
  • List(versions).Sort() sorts by SemVer precedence with an Original tie-breaker.

Rendering:

  • String() preserves the input v/V style and includes build metadata.
  • SemVer() uses no prefix and includes build metadata.
  • StringV() uses lowercase v and includes build metadata.
  • Canonical() uses lowercase v and removes build metadata.
  • MajorStr(), MajorMinorStr(), ReleaseStr() use lowercase v and 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, and json.Unmarshaler.

Related projects

For Calendar Versioning, use the companion WoozyMasta/calver library.

About

Fast, dependency-free Go library for parsing, validating, rendering, and comparing Semantic Versioning (SemVer 2.0.0) values with explicit formats

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages