/go-semver

Semantic Versioning Library for Google Go.

Primary LanguageGoMIT LicenseMIT

go-semver - Semantic versioning library

wercker status Coverage Status GoDoc

go-semver is a semantic versioning library for Go.

For package documentation, refer to the GoDoc badge above.

Installation

go get gopkg.in/zhevron/go-semver.v0/semver

Usage

Parse a version number

package main

import (
  "fmt"

  "gopkg.in/zhevron/go-semver.v0/semver"
)

func main() {
  // A semver string
  versionString := "2.0.0-beta1+20150505"

  // Parse it into a semver.Version
  version, err := semver.ParseVersion(versionString)
  if err != nil {
    panic(err)
  }

  // Print the major, minor and patch versions
  fmt.Printf("Version: %d.%d.%d\n", version.Major, version.Minor, version.Patch)
}

Constraints

package main

import (
  "fmt"

  "gopkg.in/zhevron/go-semver.v0/semver"
)

func main() {
  // A semver string
  versionString := "2.0.0-beta1+20150505"

  // A version constraint string
  constraintString := ">=1.5.0"

  // Parse the version string into a semver.Version
  version, err := semver.ParseVersion(versionString)
  if err != nil {
    panic(err)
  }

  // Parse the constraint string into a semver.Constraint
  constraint, err := semver.ParseConstraint(constraintString)
  if err != nil {
    panic(err)
  }

  // Check if the version matches the constraint
  if constraint.Match(version) {
    fmt.Println("The version matches the constraint")
  } else {
    fmt.Println("The version does NOT match the constraint")
  }
}

License

go-semver is licensed under the MIT license.