semver

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package semver is a standard-library-only Go port of the npm "semver" package (https://www.npmjs.com/package/semver), the reference implementation of Semantic Versioning 2.0.0 (https://semver.org) used throughout the Node and Express ecosystems. It parses, compares, increments, coerces and range-matches version strings with the same semantics JavaScript projects rely on, so tooling that reasons about dependency versions can be ported to Go without pulling in a third-party dependency.

A version is Major.Minor.Patch with optional dot-separated prerelease and build-metadata identifiers, for example "1.2.3-alpha.1+build.5". Parse turns a string into a *Version; MustParse panics instead of returning an error and is convenient for constants and tests. Valid reports whether a string is a well-formed version, and Clean normalizes surrounding whitespace and an optional leading "v" or "=".

Ordering follows the spec precisely: numeric fields compare numerically, a version with a prerelease has lower precedence than the same version without one, prerelease identifiers compare left to right (numeric identifiers numerically, alphanumeric identifiers lexically in ASCII order, and numeric identifiers always rank below alphanumeric ones), and build metadata is ignored for precedence. Compare returns -1, 0 or +1; the boolean helpers GT, GTE, LT, LTE, EQ and NEQ wrap it, and Sort orders a slice ascending.

Inc and the IncMajor/IncMinor/IncPatch methods produce the next version for a release level, resetting the lower fields and clearing prerelease and build metadata exactly as npm's semver does. Coerce extracts the first version-shaped run of digits from arbitrary text ("v2 release" -> "2.0.0").

Range matching is provided by Satisfies and the Range type, which understand the common npm range grammar: plain comparators (">=1.2.0", "<2.0.0", "=1.0.0" and a bare "1.2.3"), caret ranges ("^1.2.3"), tilde ranges ("~1.2.0"), x-ranges ("1.x", "1.2.*", "*"), hyphen ranges ("1.2.3 - 2.3.4"), space- or comma-separated AND terms within a set, and "||" separating alternative sets. Prerelease versions only satisfy a range when a comparator in the same tuple names the same Major.Minor.Patch with its own prerelease, matching npm's default (includePrerelease=false) behaviour.

Everything is deterministic and depends only on the standard library.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidVersion = errors.New("semver: invalid version")

ErrInvalidVersion is returned by Parse and the string-based helpers when the input is not a valid semantic version.

Functions

func Clean

func Clean(s string) string

Clean returns the canonical string form of s (dropping a leading "v"/"=" and surrounding whitespace) when s is a valid version, or "" when it is not.

func Coerce

func Coerce(s string) (string, error)

Coerce extracts the first version-like sequence from arbitrary text and returns it as a canonical version string. Missing minor or patch fields default to zero, so "v2" becomes "2.0.0" and "1.2.x build" becomes "1.2.0". It returns ErrInvalidVersion when no numeric component is found.

func Compare

func Compare(a, b string) int

Compare parses two version strings and returns -1, 0 or +1 as a is less than, equal to, or greater than b. It panics if either string is invalid; use Valid to check first when the inputs are untrusted.

func EQ

func EQ(a, b string) bool

EQ reports whether version strings a and b have equal precedence.

func GT

func GT(a, b string) bool

GT reports whether version string a has higher precedence than b.

func GTE

func GTE(a, b string) bool

GTE reports whether version string a has precedence greater than or equal to b.

func Inc

func Inc(s, release string) (string, error)

Inc returns the version string incremented at the given release level, which must be "major", "minor" or "patch". It returns an error for an invalid version or an unknown release level.

func LT

func LT(a, b string) bool

LT reports whether version string a has lower precedence than b.

func LTE

func LTE(a, b string) bool

LTE reports whether version string a has precedence less than or equal to b.

func Major

func Major(s string) (uint64, error)

Major returns the major field of a version string.

func MaxSatisfying

func MaxSatisfying(versions []string, constraint string) (string, bool)

MaxSatisfying returns the highest version in versions that satisfies the range, or "" (and ok=false) if none do. Invalid version strings are skipped.

func MinSatisfying

func MinSatisfying(versions []string, constraint string) (string, bool)

MinSatisfying returns the lowest version in versions that satisfies the range, or "" (and ok=false) if none do. Invalid version strings are skipped.

func Minor

func Minor(s string) (uint64, error)

Minor returns the minor field of a version string.

func NEQ

func NEQ(a, b string) bool

NEQ reports whether version strings a and b differ in precedence.

func Patch

func Patch(s string) (uint64, error)

Patch returns the patch field of a version string.

func Prerelease

func Prerelease(s string) (string, error)

Prerelease returns the dot-joined prerelease identifiers of a version string ("" when there are none).

func Satisfies

func Satisfies(version, constraint string) bool

Satisfies reports whether the version string satisfies the range string. It returns false if either input is invalid.

func Sort

func Sort(versions []string)

Sort sorts a slice of version strings ascending, in place. Invalid strings cause a panic; validate untrusted input first.

func Valid

func Valid(s string) bool

Valid reports whether s is a well-formed semantic version.

Types

type Range

type Range struct {
	// contains filtered or unexported fields
}

Range is a compiled semantic-version range: a disjunction (OR) of tuples, where each tuple is a conjunction (AND) of comparators. It matches the common npm range grammar (see the package documentation). The zero Range matches nothing; construct one with ParseRange.

func ParseRange

func ParseRange(expr string) (*Range, error)

ParseRange compiles a range expression (for example "^1.2.0", ">=1.0.0 <2.0.0", "1.x || 2.x", "1.2.3 - 2.3.4"). It returns an error when the expression cannot be parsed.

func (*Range) String

func (r *Range) String() string

String returns the original range expression the Range was parsed from.

func (*Range) Test

func (r *Range) Test(v *Version) bool

Test reports whether the version satisfies the range. A prerelease version only matches when a comparator naming the same Major.Minor.Patch also carries a prerelease, mirroring npm's default behaviour.

type Version

type Version struct {
	// Major, Minor and Patch are the three numeric release fields.
	Major uint64
	Minor uint64
	Patch uint64
	// Prerelease holds the dot-separated identifiers following a '-', in order
	// (empty when the version has no prerelease).
	Prerelease []string
	// Build holds the dot-separated build-metadata identifiers following a '+',
	// in order (empty when the version has no build metadata). Build metadata
	// does not affect precedence.
	Build []string
}

Version is a parsed semantic version. The zero Version is 0.0.0 with no prerelease or build identifiers.

func MustParse

func MustParse(s string) *Version

MustParse is like Parse but panics if the version is invalid. It is intended for package-level variables and tests where the input is known good.

func Parse

func Parse(s string) (*Version, error)

Parse parses a semantic version string, tolerating an optional leading "v" or "=" and surrounding whitespace. It returns ErrInvalidVersion (wrapped) when the string is not a valid version.

func (*Version) Compare

func (v *Version) Compare(o *Version) int

Compare returns -1, 0 or +1 as v is less than, equal to, or greater than o in semantic-version precedence. Build metadata is ignored.

func (*Version) Core

func (v *Version) Core() *Version

Core returns a copy of the version with prerelease and build metadata stripped, i.e. just Major.Minor.Patch.

func (*Version) Equal

func (v *Version) Equal(o *Version) bool

Equal reports whether v and o have equal precedence (build metadata ignored).

func (*Version) GreaterThan

func (v *Version) GreaterThan(o *Version) bool

GreaterThan reports whether v has higher precedence than o.

func (*Version) IncMajor

func (v *Version) IncMajor() *Version

IncMajor returns a new version with Major incremented and the lower fields, prerelease and build metadata cleared.

func (*Version) IncMinor

func (v *Version) IncMinor() *Version

IncMinor returns a new version with Minor incremented, Patch cleared and prerelease and build metadata cleared.

func (*Version) IncPatch

func (v *Version) IncPatch() *Version

IncPatch returns a new version with Patch incremented and prerelease and build metadata cleared. A prerelease version is promoted to its release form without incrementing Patch, matching npm semver.

func (*Version) LessThan

func (v *Version) LessThan(o *Version) bool

LessThan reports whether v has lower precedence than o.

func (*Version) String

func (v *Version) String() string

String returns the canonical string representation of the version, including any prerelease and build-metadata identifiers.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL