semver

Go Reference GitHub

Package semver contains Semantic Versioning 2.0.0 support for Go.

Installation

go get github.com/solsw/semver

SemVer

A version is represented by the SemVer struct:

type SemVer struct {
	Major      int64  // https://semver.org/#spec-item-8
	Minor      int64  // https://semver.org/#spec-item-7
	Patch      int64  // https://semver.org/#spec-item-6
	PreRelease string // https://semver.org/#spec-item-9
	Build      string // https://semver.org/#spec-item-10
}

The zero value of SemVer is the "0.0.0" version.

SemVer implements fmt.Stringer, encoding.TextMarshaler and encoding.TextUnmarshaler, so it can be used directly with fmt and with JSON, XML, etc.

String and MarshalText do not validate the receiver. An invalid SemVer (e.g. with negative fields) may produce an invalid version string. Use IsValid to check it beforehand.

Usage

package main

import (
	"fmt"

	"github.com/solsw/semver"
)

func main() {
	v, err := semver.Parse("1.2.3-rc.1+build.7")
	if err != nil {
		panic(err)
	}
	fmt.Println(v.Major, v.Minor, v.Patch) // 1 2 3
	fmt.Println(v.PreRelease)              // rc.1
	fmt.Println(v.Build)                   // build.7
	fmt.Println(v)                         // 1.2.3-rc.1+build.7

	less, err := v.LessThan(semver.SemVer{Major: 1, Minor: 2, Patch: 3})
	if err != nil {
		panic(err)
	}
	fmt.Println(less) // true (a pre-release is lower than the release)
}

API

Functions

Methods