semver

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package semver provides semantic versioning utilities with support for partial version matching (e.g., "2.1" matches "2.1.x").

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compare

func Compare(a, b *Version) int

Compare compares two versions. Returns -1 if a < b, 0 if a == b, 1 if a > b. Prereleases are considered lower than releases (1.0.0-alpha < 1.0.0).

func FilterValid

func FilterValid(versions []string) []string

FilterValid returns only valid semver strings from the input slice.

func IsValid

func IsValid(s string) bool

IsValid checks if a string is a valid semantic version.

func Match

func Match(versions []string, target string) (string, bool)

Match finds the best matching version for a target pattern. The target can be a full version ("2.1.3") or partial ("2.1" or "2"). Returns the highest version that matches the pattern. Prereleases are excluded unless the target is an exact match.

func Sort

func Sort(versions []*Version)

Sort sorts a slice of versions in ascending order.

func SortDesc

func SortDesc(versions []*Version)

SortDesc sorts a slice of versions in descending order.

func SortStrings

func SortStrings(versions []string) []string

SortStrings sorts version strings in ascending order. Invalid versions are filtered out.

func SortStringsDesc

func SortStringsDesc(versions []string) []string

SortStringsDesc sorts version strings in descending order. Invalid versions are filtered out.

Types

type Version

type Version struct {
	Major      int    // Major version number (required)
	Minor      int    // Minor version number (-1 if unspecified)
	Patch      int    // Patch version number (-1 if unspecified)
	Prerelease string // Prerelease identifier (e.g., "alpha", "beta.1")
	Build      string // Build metadata (e.g., "build.123")
	Original   string // Original string representation
}

Version represents a parsed semantic version. Supports partial versions where Minor or Patch may be unspecified (-1).

func MustParse

func MustParse(s string) *Version

MustParse parses a semver string and panics on error. Use only for known-good version strings.

func Parse

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

Parse parses a semver string into a Version struct. Supports partial versions: "2", "2.1", "2.1.3", "2.1.3-beta+build"

func (*Version) HasMinor

func (v *Version) HasMinor() bool

HasMinor returns true if the minor version is specified.

func (*Version) HasPatch

func (v *Version) HasPatch() bool

HasPatch returns true if the patch version is specified.

func (*Version) HasPrerelease

func (v *Version) HasPrerelease() bool

HasPrerelease returns true if a prerelease identifier is present.

func (*Version) String

func (v *Version) String() string

String returns the canonical string representation of the version.

Jump to

Keyboard shortcuts

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