semver

package
v1.2.9 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2025 License: MIT Imports: 4 Imported by: 0

README

Semver

A SemVer Package is a package that implements the Semantic Versioning (SemVer) specification. SemVer is a widely adopted standard for versioning software libraries and applications, making it easier for developers to understand when and what changes have been made to a package.



Installation

go get oss.nandlabs.io/golly/semver

Features

  • Adheres to the SemVer 2.0.0 specification
  • Easy to use API for parsing, comparing and generating SemVer versions
  • Supports pre-release and build metadata
  • Written in modern Golang and follows best practices

Usage

Here is an example of how to use the SemVer package in a Golang project:

package main

import (
	"fmt"
	"oss.nandlabs.io/golly/semver"
)

func main() {
	version, err := semver.Parse("v1.2.3")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("Major :: %d", version.CurrentMajor())
	fmt.Printf("Minor :: %d", version.CurrentMinor())
	fmt.Printf("Patch :: %d", version.CurrentPatch())

	metadataVersion, err := semver.Parse("v1.2.3-SNAPSHOT")
	if err != nil {
		fmt.Println(err)
    }
	fmt.Printf("Major :: %d", metadataVersion.CurrentMajor())
	fmt.Printf("Minor :: %d", metadataVersion.CurrentMinor())
	fmt.Printf("Patch :: %d", metadataVersion.CurrentPatch())
	fmt.Printf("Pre-Release :: %s", metadataVersion.CurrentPreRelease)
}

Documentation

Overview

Package semver provides a set of utilities for working with semantic versions in Go.

Index

Constants

View Source
const (
	RegexSemver     = `^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?(\+([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?$`
	RegexPreRelease = `^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)-([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)`
)

Variables

This section is empty.

Functions

func CompareRaw

func CompareRaw(ver1, ver2 string) (int, error)

CompareRaw returns three values -1, 0, +1 -1 denotes ver1 < ver2 0 denotes invalid input +1 denotes ver1 > ver2

Types

type SemVer

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

SemVer is a struct that represents a semantic version with major, minor, patch, pre-release and build metadata. For more information on semantic versioning, see https://semver.org/spec/v2.0.0.html

func Parse

func Parse(input string) (*SemVer, error)

Parse will parse the given input string and provide a semver struct If the input string is not a valid semver string, an error will be returned

func (*SemVer) Compare

func (s *SemVer) Compare(v *SemVer) (int, error)

Compare returns three values -1, 0, +1 -1 denotes ver1 < ver2 0 denotes invalid input +1 denotes ver1 > ver2

func (*SemVer) CurrentBuild added in v1.0.7

func (s *SemVer) CurrentBuild() string

Build returns the build metadata of the SemVer struct.

func (*SemVer) CurrentMajor added in v1.0.7

func (s *SemVer) CurrentMajor() int

Major returns the major version of the SemVer struct.

func (*SemVer) CurrentMinor added in v1.0.7

func (s *SemVer) CurrentMinor() int

Minor returns the minor version of the SemVer struct.

func (*SemVer) CurrentPatch added in v1.0.7

func (s *SemVer) CurrentPatch() int

Patch returns the patch version of the SemVer struct.

func (*SemVer) CurrentPreRelease added in v1.0.7

func (s *SemVer) CurrentPreRelease() string

PreRelease returns the pre-release metadata of the SemVer struct.

func (*SemVer) IsCurrentPreRelease added in v1.0.7

func (s *SemVer) IsCurrentPreRelease() bool

func (*SemVer) NextMajor

func (s *SemVer) NextMajor() *SemVer

Next Major Increments the major version

func (*SemVer) NextMinor

func (s *SemVer) NextMinor() *SemVer

NextMinor Increments the minor version

func (*SemVer) NextPatch

func (s *SemVer) NextPatch() *SemVer

NextPatch Increments the patch version

func (*SemVer) NextPreRelease added in v1.0.7

func (s *SemVer) NextPreRelease(tag string) *SemVer

NextPreRelease

func (*SemVer) String

func (s *SemVer) String() string

New creates a new SemVer struct with the given major, minor, and patch versions.

Jump to

Keyboard shortcuts

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