semver

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: BSD-3-Clause Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package semver parses, compares, and evaluates Semantic Versioning 2.0.0 versions and version constraints on top of golang.org/x/mod/semver.

Unlike golang.org/x/mod/semver, every function in this package accepts version strings with or without a leading "v" prefix.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidVersion reports that a string is not a valid semantic version.
	ErrInvalidVersion = errors.New("invalid semantic version")
	// ErrInvalidConstraint reports that a string is not a valid version constraint.
	ErrInvalidConstraint = errors.New("invalid version constraint")
)

Functions

func Build

func Build[T Input](v T) string

Build returns the build metadata suffix of v including its leading "+", or "" if v is invalid or has no build metadata.

func Canonical

func Canonical[T Input](v T) string

Canonical returns the canonical "vMAJOR.MINOR.PATCH[-prerelease]" form of v without build metadata, or "" if v is not valid.

func Compare

func Compare[T Input](a, b T) int

Compare compares two versions (strings or parsed Version values) according to semantic version precedence. Invalid version strings compare less than valid ones, and two invalid strings compare equal.

func Filter

func Filter[S ~[]E, E Input](c *Constraints, items S) S

Filter returns a new slice containing the elements of items that satisfy c.

func IsStable

func IsStable[T Input](v T) bool

IsStable reports whether v is a valid semantic version with no prerelease.

func IsValid

func IsValid[T Input](v T) bool

IsValid reports whether v is a valid semantic version (with an optional leading "v" and optional minor/patch components).

func IsValidStrict

func IsValidStrict[S ~string](v S) bool

IsValidStrict reports whether v is a valid complete MAJOR.MINOR.PATCH semantic version (with an optional leading "v").

func Major

func Major[T Input](v T) string

Major returns the "vMAJOR" prefix of v, or "" if v is not valid.

func MajorMinor

func MajorMinor[T Input](v T) string

MajorMinor returns the "vMAJOR.MINOR" prefix of v, or "" if v is not valid.

func Max

func Max[T Input](a, b T) T

Max returns whichever of a and b has higher semantic version precedence.

func Prerelease

func Prerelease[T Input](v T) string

Prerelease returns the prerelease suffix of v including its leading "-", or "" if v is invalid or has no prerelease.

func Sort

func Sort[S ~[]E, E Input](list S)

Sort sorts a slice of version strings or Version values in ascending semantic version order.

func SortBy

func SortBy[S ~[]E, E any, K Input](items S, key func(E) K)

SortBy sorts items in ascending semantic version order using key to extract each element's version string or Version value.

Types

type Constraints

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

Constraints represents a parsed semantic version constraint expression supporting comparators (=, ==, !=, <, <=, >, >=), wildcards (*, x, X), tilde (~, ~>), caret (^), hyphen ranges (A - B), AND (space/comma), and OR (||) groups.

func MustParseConstraint

func MustParseConstraint[S ~string](raw S) *Constraints

MustParseConstraint parses raw with NewConstraint and panics on error.

func NewConstraint

func NewConstraint[S ~string](raw S) (*Constraints, error)

NewConstraint parses a semantic version constraint expression.

func (*Constraints) Check

func (c *Constraints) Check(v Version) bool

Check reports whether v satisfies c.

Prerelease versions are only matched by an OR-group when at least one comparator in that group specifies a prerelease identifier.

func (*Constraints) CheckString

func (c *Constraints) CheckString(raw string) bool

CheckString parses raw with Parse and reports whether it satisfies c.

func (*Constraints) String

func (c *Constraints) String() string

String returns the original constraint expression.

type Input

type Input interface {
	~string | Version
}

Input constrains generic semver helpers to version strings or parsed Version values.

type Version

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

Version represents a parsed Semantic Version 2.0.0 value.

func MustParse

func MustParse[S ~string](raw S) Version

MustParse parses raw with Parse and panics if it is invalid.

func MustParseStrict

func MustParseStrict[S ~string](raw S) Version

MustParseStrict parses raw with ParseStrict and panics if it is invalid.

func Parse

func Parse[S ~string](raw S) (Version, error)

Parse parses a semantic version string. An optional leading "v" is allowed, and missing minor or patch components default to 0 (for example, "1.2" parses as 1.2.0 and "v1" parses as 1.0.0).

func ParseStrict

func ParseStrict[S ~string](raw S) (Version, error)

ParseStrict parses a complete Semantic Version 2.0.0 string requiring all three core components (MAJOR.MINOR.PATCH). An optional leading "v" is allowed.

func (Version) Canonical

func (v Version) Canonical() string

Canonical returns the canonical "vMAJOR.MINOR.PATCH[-prerelease]" string without build metadata.

func (Version) Compare

func (v Version) Compare(other Version) int

Compare compares v and other according to Semantic Versioning 2.0.0 precedence rules, ignoring build metadata. It returns -1 if v < other, 0 if v == other, and +1 if v > other.

func (Version) Equal

func (v Version) Equal(other Version) bool

Equal reports whether v and other have equal SemVer precedence.

func (Version) GreaterThan

func (v Version) GreaterThan(other Version) bool

GreaterThan reports whether v has higher precedence than other.

func (Version) GreaterThanEqual

func (v Version) GreaterThanEqual(other Version) bool

GreaterThanEqual reports whether v has higher or equal precedence to other.

func (Version) IsPrerelease

func (v Version) IsPrerelease() bool

IsPrerelease reports whether v has a prerelease identifier.

func (Version) IsStable

func (v Version) IsStable() bool

IsStable reports whether v has no prerelease identifier.

func (Version) LessThan

func (v Version) LessThan(other Version) bool

LessThan reports whether v has lower precedence than other.

func (Version) LessThanEqual

func (v Version) LessThanEqual(other Version) bool

LessThanEqual reports whether v has lower or equal precedence to other.

func (Version) Major

func (v Version) Major() uint64

Major returns the major version number.

func (Version) MajorMinor

func (v Version) MajorMinor() string

MajorMinor returns the "vMAJOR.MINOR" prefix of v.

func (Version) MarshalText

func (v Version) MarshalText() ([]byte, error)

MarshalText encodes v as its canonical SemVer string.

func (Version) Metadata

func (v Version) Metadata() string

Metadata returns the build metadata without a leading "+".

func (Version) Minor

func (v Version) Minor() uint64

Minor returns the minor version number.

func (Version) Original

func (v Version) Original() string

Original returns the original input string used to construct v, or v.String() when v was not parsed from a string.

func (Version) Patch

func (v Version) Patch() uint64

Patch returns the patch version number.

func (Version) Prerelease

func (v Version) Prerelease() string

Prerelease returns the prerelease identifier without a leading "-".

func (Version) String

func (v Version) String() string

String returns the canonical Semantic Version 2.0.0 string without a leading "v".

func (*Version) UnmarshalText

func (v *Version) UnmarshalText(text []byte) error

UnmarshalText decodes a semantic version string into v.

Jump to

Keyboard shortcuts

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