semver

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2025 License: MIT Imports: 6 Imported by: 0

README

go-semver

A lightweight and idiomatic Go package to parse and compare semantic versions based on the Semantic Versioning 2.0.0 specification.

✨ Features

  • Parse semantic version strings (MAJOR.MINOR.PATCH[-PRERELEASE][+BUILD])
  • Compare versions with full support for precedence rules
  • Handle pre-release and build metadata
  • Determine ordering between two versions
  • Idiomatic Go API

📦 Installation

go get github.com/Nidal-Bakir/go-semver

🛠️ Usage

package main

import (
    "fmt"
    "github.com/Nidal-Bakir/go-semver"
)

func main() {
    v1, _ := semver.Parse("1.2.3-alpha.1")
    v2, _ := semver.Parse("1.2.3")

    if v1.IsLess(v2) {
        fmt.Println(v1, "is less than", v2)
    }
}

🔍 Version Comparison Rules

This package follows the precedence rules defined in the Semantic Versioning 2.0.0 spec, including:

  • Precedence is determined by the major, minor, and patch versions, in that order.

  • Pre-release versions have lower precedence than the associated normal version.

  • Build metadata does not affect version precedence.

✅ Example Comparisons

Version A Version B Result
1.0.0 2.0.0 A < B
1.0.0-alpha 1.0.0 A < B
1.0.0-alpha 1.0.0-alpha.1 A < B
1.2.3+build1 1.2.3+build2 A == B

Documentation

Overview

Semantic Versioning 2.0.0

see https://semver.org

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidSemVerSyntax = errors.New("invalid semver syntax")
)

Functions

func Compare added in v1.1.0

func Compare(v1, v2 string) (int, error)

func IsValid added in v1.1.0

func IsValid(v string) bool

func Sort added in v1.1.0

func Sort(slice []SemVer)

Sort sorts a list of semantic versions in a ascending order

func SortStr added in v1.1.0

func SortStr(slice []string) error

Sort sorts a list of string semantic versions in a ascending order.

Types

type SemVer

type SemVer struct {
	Major         int
	Minor         int
	Patch         int
	PreRelease    string
	BuildMetadata string
}

func MustParse added in v1.1.0

func MustParse(semverStr string) SemVer

func Parse

func Parse(semverStr string) (SemVer, error)

func (SemVer) Compare added in v1.1.0

func (s SemVer) Compare(other SemVer) int

Compare returns

-1 if this is less than other,
 0 if this equals other,
+1 if this is greater than other.

func (SemVer) IsEquql

func (s SemVer) IsEquql(o SemVer) bool

func (SemVer) IsGrater

func (s SemVer) IsGrater(o SemVer) bool

func (SemVer) IsGraterOrEquql

func (s SemVer) IsGraterOrEquql(o SemVer) bool

func (SemVer) IsLess

func (s SemVer) IsLess(o SemVer) bool

func (SemVer) IsLessOrEquql

func (s SemVer) IsLessOrEquql(o SemVer) bool

func (SemVer) IsPreRelease

func (s SemVer) IsPreRelease() bool

func (SemVer) String

func (s SemVer) String() string

Jump to

Keyboard shortcuts

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