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