semver for golang Build Status GoDoc Coverage Status

semver is a Semantic Versioning library written in golang. It fully covers spec version 2.0.0.


$ go get
v1, err := semver.New("1.0.0-beta")
v2, err := semver.New("2.0.0-beta")

Also check the GoDocs.

Why should I use this lib?

  • Fully spec compatible
  • No reflection
  • No regex
  • Fully tested (Coverage >99%)
  • Readable parsing/validation errors
  • Fast (See Benchmarks)
  • Only Stdlib


  • Parsing and validation at all levels
  • Comparator-like comparisons
  • Compare Helper Methods
  • InPlace manipulation
  • database/sql compatible (sql.Scanner/Valuer)


Have a look at full examples in examples/main.go


v, err := semver.New("0.0.1-alpha.preview+123.github")
fmt.Printf("Major: %d\n", v.Major)
fmt.Printf("Minor: %d\n", v.Minor)
fmt.Printf("Patch: %d\n", v.Patch)
fmt.Printf("Pre: %s\n", v.Pre)
fmt.Printf("Build: %s\n", v.Build)

// Prerelease versions array
if len(v.Pre) > 0 {
    fmt.Println("Prerelease versions:")
    for i, pre := range v.Pre {
        fmt.Printf("%d: %q\n", i, pre)

// Build meta data array
if len(v.Build) > 0 {
    fmt.Println("Build meta data:")
    for i, build := range v.Build {
        fmt.Printf("%d: %q\n", i, build)

v001, err := semver.New("0.0.1")
// Compare using helpers: v.GT(v2), v.LT, v.GTE, v.LTE
v001.GT(v) == true
v.LT(v001) == true
v.GTE(v) == true
v.LTE(v) == true

// Or use v.Compare(v2) for comparisons (-1, 0, 1):
v001.Compare(v) == 1
v.Compare(v001) == -1
v.Compare(v) == 0

// Manipulate Version in place:
v.Pre[0], err = semver.NewPRVersion("beta")
if err != nil {
    fmt.Printf("Error parsing pre release version: %q", err)

fmt.Println("\nValidate versions:")
v.Build[0] = "?"

err = v.Validate()
if err != nil {
    fmt.Printf("Validation failed: %s\n", err)


BenchmarkParseSimple        5000000           442    ns/op
BenchmarkParseComplex       1000000          2441    ns/op
BenchmarkParseAverage       1000000          1497    ns/op
BenchmarkValidateSimple   500000000             4.83 ns/op
BenchmarkValidateComplex    1000000          1236    ns/op
BenchmarkValidateAverage    5000000           580    ns/op
BenchmarkCompareSimple    500000000             5.43 ns/op
BenchmarkCompareComplex   100000000            26.3  ns/op
BenchmarkCompareAverage   100000000            29.6  ns/op

See benchmark cases at semver_test.go


I simply couldn't find any lib supporting the full spec. Others were just wrong or used reflection and regex which i don't like.


Feel free to make a pull request. For bigger changes create a issue first to discuss about it.


See LICENSE file.

Expand ▾ Collapse ▴




This section is empty.


View Source
var SPEC_VERSION = Version{
	Major: 2,
	Minor: 0,
	Patch: 0,

    Latest fully supported spec version


    func NewBuildVersion

    func NewBuildVersion(s string) (string, error)

      Creates a new valid build version


      type PRVersion

      type PRVersion struct {
      	VersionStr string
      	VersionNum uint64
      	IsNum      bool

        PreRelease Version

        func NewPRVersion

        func NewPRVersion(s string) (*PRVersion, error)

          Creates a new valid prerelease version

          func (PRVersion) Compare

          func (v PRVersion) Compare(o *PRVersion) int

            Compares PreRelease Versions v to o: -1 == v is less than o 0 == v is equal to o 1 == v is greater than o

            func (PRVersion) IsNumeric

            func (v PRVersion) IsNumeric() bool

              Is pre release version numeric?

              func (PRVersion) String

              func (v PRVersion) String() string

                PreRelease version to string

                type Version

                type Version struct {
                	Major uint64
                	Minor uint64
                	Patch uint64
                	Pre   []*PRVersion
                	Build []string //No Precendence

                func New

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

                  Alias for Parse, parses version string and returns a validated Version or error

                  func Parse

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

                    Parses version string and returns a validated Version or error

                    func (Version) Compare

                    func (v Version) Compare(o *Version) int

                      Compares Versions v to o: -1 == v is less than o 0 == v is equal to o 1 == v is greater than o

                      func (Version) GT

                      func (v Version) GT(o *Version) bool

                        Checks if v is greater than o.

                        func (Version) GTE

                        func (v Version) GTE(o *Version) bool

                          Checks if v is greater than or equal to o.

                          func (Version) LT

                          func (v Version) LT(o *Version) bool

                            Checks if v is less than o.

                            func (Version) LTE

                            func (v Version) LTE(o *Version) bool

                              Checks if v is less than or equal to o.

                              func (*Version) Scan

                              func (v *Version) Scan(src interface{}) (err error)

                                Scan implements the database/sql.Scanner interface.

                                func (Version) String

                                func (v Version) String() string

                                  Version to string

                                  func (Version) Validate

                                  func (v Version) Validate() error

                                    Validates v and returns error in case

                                    func (Version) Value

                                    func (s Version) Value() (driver.Value, error)

                                      Value implements the database/sql/driver.Valuer interface.

                                      Source Files


                                      Path Synopsis