1# Versioning Library for Go 2[![Build Status](https://circleci.com/gh/hashicorp/go-version/tree/master.svg?style=svg)](https://circleci.com/gh/hashicorp/go-version/tree/master) 3[![GoDoc](https://godoc.org/github.com/hashicorp/go-version?status.svg)](https://godoc.org/github.com/hashicorp/go-version) 4 5go-version is a library for parsing versions and version constraints, 6and verifying versions against a set of constraints. go-version 7can sort a collection of versions properly, handles prerelease/beta 8versions, can increment versions, etc. 9 10Versions used with go-version must follow [SemVer](http://semver.org/). 11 12## Installation and Usage 13 14Package documentation can be found on 15[GoDoc](http://godoc.org/github.com/hashicorp/go-version). 16 17Installation can be done with a normal `go get`: 18 19``` 20$ go get github.com/hashicorp/go-version 21``` 22 23#### Version Parsing and Comparison 24 25```go 26v1, err := version.NewVersion("1.2") 27v2, err := version.NewVersion("1.5+metadata") 28 29// Comparison example. There is also GreaterThan, Equal, and just 30// a simple Compare that returns an int allowing easy >=, <=, etc. 31if v1.LessThan(v2) { 32 fmt.Printf("%s is less than %s", v1, v2) 33} 34``` 35 36#### Version Constraints 37 38```go 39v1, err := version.NewVersion("1.2") 40 41// Constraints example. 42constraints, err := version.NewConstraint(">= 1.0, < 1.4") 43if constraints.Check(v1) { 44 fmt.Printf("%s satisfies constraints %s", v1, constraints) 45} 46``` 47 48#### Version Sorting 49 50```go 51versionsRaw := []string{"1.1", "0.7.1", "1.4-beta", "1.4", "2"} 52versions := make([]*version.Version, len(versionsRaw)) 53for i, raw := range versionsRaw { 54 v, _ := version.NewVersion(raw) 55 versions[i] = v 56} 57 58// After this, the versions are properly sorted 59sort.Sort(version.Collection(versions)) 60``` 61 62## Issues and Contributing 63 64If you find an issue with this library, please report an issue. If you'd 65like, we welcome any contributions. Fork this library and submit a pull 66request. 67