Masterminds/semver: Go Semantic Version Parsing and Constraints
Work with Semantic Versions in Go
At a glance
- What is it?
- Masterminds/semver is a Go library for parsing, sorting, comparing, and validating semantic versions, including constraint expressions used by package and release tooling. Its main trade-off is that version ordering follows the SemVer specification while range checking follows conventions associated with npm and Cargo.
- Who is it for?
- Adopt Masterminds/semver when a Go tool needs semantic-version objects or readable constraints, especially when its npm and Cargo-style range rules match the surrounding ecosystem. Do not choose it without checking strict parsing, coercion, and pre-release behavior against your release policy.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 70 days ago.
- What is it written in?
- Mainly Go, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on October 23, 2026, and from our analysis. They are not legal advice.
Editorial analysis
What Masterminds/semver Solves
Masterminds/semver solves a recurring problem in Go applications that consume version strings. A value such as "1.2.3-beta.1+build345" is not just text to sort lexically. Its major, minor, patch, pre-release, and build metadata have defined roles, and applications need to compare those values consistently. The package turns version input into a `Version` object that can be compared, sorted, formatted, and checked against constraints.
The intended users are Go developers building dependency resolvers, release tools, deployment checks, package managers, and applications that validate version requirements. The repository is a library rather than a command-line application or service. Its scope is deliberately narrow: it operates on individual versions and constraint expressions. It does not fetch packages, resolve dependencies, contact registries, or install releases.
The library supports an optional `v` prefix and can work with versions that are not fully compliant. That matters for tools receiving input from different ecosystems. A Go program may need to accept `v1.2`, `1.2`, or a CalVer-style value rather than rejecting every input that lacks the exact three-part form. The package offers strict parsing for callers that want specification-only behavior and a coercive parser for callers that need broader input tolerance.
Masterminds/semver is therefore best understood as parsing and policy infrastructure. It gives Go code a common representation, but the application still decides which inputs are trusted, whether coercion is acceptable, and how range failures should be reported.
Strict and Coerced Go Version Parsing
The package exposes two parsing paths. `StrictNewVersion` accepts valid Semantic Versioning 2 values, while `NewVersion` attempts to coerce non-compliant input. The README gives `v1.2` as an example of input that the coercive parser converts to `1.2.0`. It also accepts a leading `v`, and the resulting `Version` retains access to the original string.
That distinction affects application design. Code that receives release metadata from a controlled system can use strict parsing to reject malformed values. Code that receives tags or user-provided versions may prefer `NewVersion`, but it must decide whether normalization is acceptable. The package-level `CoerceNewVersion` variable controls coercion and is true by default. Setting it to false reduces the coercion performed. This is a global setting, so applications should account for that when different parsing policies are needed within one process.
The library also provides `DetailedNewVersionErrors`. According to the README, it has an effect only when coercion is disabled. Disabling detailed errors reduces parsing work but gives less information when a version fails. These controls make the parser configurable, but they also place policy outside individual calls, which can be a design constraint for services that must distinguish trusted and untrusted input.
A parsed version exposes its component parts, supports comparison, converts back to a string, and can return the original input. Those operations are useful when a tool must display exactly what a user supplied while still comparing normalized semantic values.
Sorting Versions with Go's sort Package
Masterminds/semver does not require a separate sorting abstraction beyond the standard library `sort` package. The documented approach parses a slice of strings into `*semver.Version` values, wraps the slice in `semver.Collection`, and sorts it. This keeps the representation compatible with Go's existing sorting model.
The README example begins with mixed version strings, including short forms such as `1.0`, `1.3`, and `2`. Because the example uses `NewVersion`, those inputs can be normalized before they enter the collection. This is a reasonable pattern for imported release data, but it means the sorting policy is tied to the parser's coercion behavior. A caller expecting strict rejection would need to use a stricter parser and handle every error before sorting.
The collection also preserves Go's familiar interface. That is useful for code that already has generic sorting code or test helpers. It does not provide a higher-level version resolver, so callers still need to decide how to handle duplicates, invalid entries, and ordering across different release sources. The repository layout, including `collection.go` and `collection_test.go`, indicates that collection behavior is a separate part of the library, while the README focuses on sorting rather than dependency resolution.
Invalid input is part of this workflow, not an afterthought. The documented example calls `t.Errorf` when parsing fails. Production code will need a different policy, such as rejecting the input set or excluding the invalid record, because passing an incomplete version into a collection would leave application behavior undefined.
Constraint Syntax and Pre-release Rules
Constraint checking is the package's largest documented feature area. `NewConstraint` accepts expressions made from space-separated or comma-separated AND comparisons, with `||` separating alternatives. The README uses `>= 1.2.3` as a basic example and explains that an expression such as `>= 1.2 < 3.0.0 || >= 4.2.3` describes two acceptable ranges.
The basic operators are `=`, `!=`, `>`, `<`, `>=`, and `<=`. Equality can also be written without an operator. The package also supports shorthand `~` and `^` ranges. The README says the implementation follows npm and Cargo because those tools use similar range conventions, and notes that other systems can differ. For example, PHP's interpretation of `^` may not match the behavior implemented here.
Pre-release handling is the main compatibility trap. Direct comparisons through `Compare`, `LessThan`, and related methods follow the Semantic Versioning comparison rules and include pre-releases. Constraint checks use different rules intended for ranges, where a pre-release can be considered invalid unless the range includes one. The README gives `-0` as a way to include pre-releases in a range. A Go tool that accepts beta or release-candidate versions must therefore test the exact constraint rather than assume that direct comparison and `Check` answer the same question.
The repository contains `constraints.go` and `constraints_test.go`, which are the relevant files for inspecting range behavior. The README excerpt stops before documenting every shorthand case, so users needing exact `~` or `^` semantics should consult those files or the Go package documentation before encoding a release policy.
Installing and First Use in a Go Module
The README does not provide a separate installation command. It directs users to import the latest major version with the module path `github.com/Masterminds/semver/v3`. The repository's `go.mod` declares Go 1.21, so the project targets a modern Go module environment.
After adding the module dependency through the Go module tooling available in your project, import the package and parse a version. The following example comes from the README. `NewVersion` accepts a leading `v`, a pre-release identifier, and build metadata. The reader should see a parsed version when the input is valid, or an error when it is not.
v, err := semver.NewVersion("1.2.3-beta.1+build345")A first constraint check creates a constraint, parses a version, and calls `Check`. The README states that `a` is true for `1.3` against `>= 1.2.3`.
c, err := semver.NewConstraint(">= 1.2.3")
if err != nil {
// Handle constraint not being parsable.
}
v, err := semver.NewVersion("1.3")
if err != nil {
// Handle version not being parsable.
}
a := c.Check(v)The Makefile documents this command for testing a repository checkout. It invokes the Go test runner with verbose output; the tests themselves are the expected confirmation when working from the source tree.
GO111MODULE=on go test -vFor library consumers, successful parsing and a `Check` result matching the intended range are the useful first checks. A surprising result may come from coercive parsing or pre-release rules rather than from ordinary precedence comparison.
Constraint Compatibility and Failure Cases
Masterminds/semver is the wrong tool when the application only needs to compare two already validated semantic versions. Direct `Version` methods cover that case without constructing a constraint. It is also unsuitable when the required range syntax follows a different ecosystem exactly. A release gate designed around PHP caret rules, for example, should not assume compatibility merely because both systems use a `^` symbol.
A common failure mode is assuming that direct comparison and constraint checking have identical pre-release semantics. That is not how this library operates. Direct comparison follows the specification, while constraints follow range-oriented package-tool conventions. Another failure mode is accepting coercive input without considering `CoerceNewVersion`. A short or otherwise non-compliant version may be normalized instead of rejected, changing what the application accepts.
The package also does not resolve dependency graphs. It can tell an application whether one parsed version satisfies a constraint, but it does not select compatible modules, download metadata, or build a complete lock file. Those jobs require surrounding application code or another package.
The README excerpt does not document serialization beyond string conversion, or a formal extension model for custom constraint operators. Code needing additional behavior should inspect `constraints.go` and its tests rather than assume such hooks exist.
License, Maintenance, and Upgrade Cost
The project uses the MIT license, as stated in the repository description and `LICENSE.txt`. That is a permissive software license, although this review does not interpret legal obligations for a particular organization. The repository is not archived. Its last push was on 2026-08-14. The README identifies 3.x.x as the stable line and directs users away from the unmaintained 1.x.x release.
The listed releases are v3.5.0 on 2026-04-30, v3.4.0 on 2025-06-27, and v3.3.1 on 2024-11-19. Based on the repository's last-push date and release history, the 3.x line has recent project activity. That is maintenance evidence, not proof that every new release will be compatible with an existing range policy. Version upgrades still require checking changelog entries and tests around parsing, coercion, and constraints.
The module path includes `/v3`, which is part of Go's major-version import convention. Consumers should keep that path when targeting the documented 3.x API. The module declares Go 1.21, so adoption can also require moving an older build environment forward.
The Makefile provides `lint`, `test-cover`, and `fuzz` targets. The fuzz target runs separate tests for `FuzzNewVersion`, `FuzzStrictNewVersion`, and `FuzzNewConstraint` for 15 seconds each. These repository checks help maintainers exercise parsing and constraint behavior, but they are not a published compatibility guarantee for downstream applications.
Editorial conclusion
Adopt Masterminds/semver when a Go tool needs semantic-version objects or readable constraints, especially when its npm and Cargo-style range rules match the surrounding ecosystem. Do not choose it without checking strict parsing, coercion, and pre-release behavior against your release policy. Verify those semantics first. The module requires Go 1.21 and uses the MIT license.
Frequently asked questions
Which import path should I use for the latest Masterminds/semver package?
The README directs users to import `github.com/Masterminds/semver/v3` for the latest version. It identifies 3.x.x as the stable line.
What is the difference between NewVersion and StrictNewVersion in Masterminds/semver?
`NewVersion` attempts to coerce non-compliant versions, including a leading `v` or a short form such as `v1.2`. `StrictNewVersion` parses only valid Semantic Versioning 2 versions.
How does Masterminds/semver handle pre-release versions in constraints?
Constraint checking can treat a pre-release as invalid when the range does not include one. The README states that including `-0` in the range provides a way to include pre-releases, while direct version comparison follows the SemVer comparison rules.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/masterminds-semver)Was this useful?