https://github.com/hashicorp/go-version
A Go (golang) library for parsing and verifying versions and version constraints.
https://github.com/hashicorp/go-version
Last synced: 3 days ago
JSON representation
A Go (golang) library for parsing and verifying versions and version constraints.
- Host: GitHub
- URL: https://github.com/hashicorp/go-version
- Owner: hashicorp
- License: mpl-2.0
- Created: 2014-03-26T05:16:55.000Z (about 11 years ago)
- Default Branch: main
- Last Pushed: 2025-04-08T09:52:52.000Z (17 days ago)
- Last Synced: 2025-04-11T11:04:38.473Z (14 days ago)
- Language: Go
- Homepage: https://pkg.go.dev/github.com/hashicorp/go-version
- Size: 113 KB
- Stars: 1,678
- Watchers: 284
- Forks: 152
- Open Issues: 38
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
- awesome-ccamel - hashicorp/go-version - A Go (golang) library for parsing and verifying versions and version constraints. (Go)
- go-awesome - hashicorp/go-version
- my-awesome - hashicorp/go-version - 04 star:1.7k fork:0.2k A Go (golang) library for parsing and verifying versions and version constraints. (Go)
README
# Versioning Library for Go

[](https://godoc.org/github.com/hashicorp/go-version)go-version is a library for parsing versions and version constraints,
and verifying versions against a set of constraints. go-version
can sort a collection of versions properly, handles prerelease/beta
versions, can increment versions, etc.Versions used with go-version must follow [SemVer](http://semver.org/).
## Installation and Usage
Package documentation can be found on
[GoDoc](http://godoc.org/github.com/hashicorp/go-version).Installation can be done with a normal `go get`:
```
$ go get github.com/hashicorp/go-version
```#### Version Parsing and Comparison
```go
v1, err := version.NewVersion("1.2")
v2, err := version.NewVersion("1.5+metadata")// Comparison example. There is also GreaterThan, Equal, and just
// a simple Compare that returns an int allowing easy >=, <=, etc.
if v1.LessThan(v2) {
fmt.Printf("%s is less than %s", v1, v2)
}
```#### Version Constraints
```go
v1, err := version.NewVersion("1.2")// Constraints example.
constraints, err := version.NewConstraint(">= 1.0, < 1.4")
if constraints.Check(v1) {
fmt.Printf("%s satisfies constraints %s", v1, constraints)
}
```#### Version Sorting
```go
versionsRaw := []string{"1.1", "0.7.1", "1.4-beta", "1.4", "2"}
versions := make([]*version.Version, len(versionsRaw))
for i, raw := range versionsRaw {
v, _ := version.NewVersion(raw)
versions[i] = v
}// After this, the versions are properly sorted
sort.Sort(version.Collection(versions))
```## Issues and Contributing
If you find an issue with this library, please report an issue. If you'd
like, we welcome any contributions. Fork this library and submit a pull
request.