Documentation ¶
Overview ¶
Package semver provides methods to validate, parse, compare and modify semantic version compliant strings.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrCorruptedVersion = errors.New("corrupted version")
var ErrNoPrerelease = errors.New("no prerelease set in version")
Functions ¶
func BuildMetadata ¶
BuildMetadata return option to set buildmetadata component. Empty string is silently ignored returning no error.
In current spec buildmetadata must consist of:
* dot separated, nonempty components of [a-zA-Z0-9-] alphabet.
func Less ¶
Less perform comparison of to specified versions. It strictly follows rules of semver specification, paragraph 11.: https://semver.org/#spec-item-11
Example ¶
versions := []string{ "1.0.0+first", "1.0.0+second", "1.0.0+3rd", "1.0.0-rc.1", "1.0.0-beta.11", "1.0.0-beta.2", "1.0.0-beta", "1.0.0-alpha.beta", "1.0.0-alpha.1", "1.0.0-alpha", } sort.SliceStable(versions, func(i, j int) bool { v1 := semver.MustParse(versions[i]) v2 := semver.MustParse(versions[j]) return semver.Less(&v1, &v2) }) for i, v := range versions { fmt.Printf("%d. %v\n", i, v) }
Output: 0. 1.0.0-alpha 1. 1.0.0-alpha.1 2. 1.0.0-alpha.beta 3. 1.0.0-beta 4. 1.0.0-beta.2 5. 1.0.0-beta.11 6. 1.0.0-rc.1 7. 1.0.0+first 8. 1.0.0+second 9. 1.0.0+3rd
func Prerelease ¶
Prerelease return option to set prerelease component. Empty string is silently ignored returning no error. Calling this option will not clear prerelease component so it may be used more than once and final result will be sum of all requests
With current spec valid request string should be:
* dot separated, nonempty components of [a-zA-Z0-9-] alphabet * if only numbers are used for particular component leading zeroes are forbidden
Valid examples:
`a.b.c`, `rc.1`, `rc01`, `alpha.beta.gamma` etc.
Types ¶
type BumpOption ¶
BumpOption is function option that changes version to newer
func NextPrerelease ¶
func NextPrerelease() BumpOption
NextPrerelease is incrementing last numeric prerelease component
func NextRelease ¶
func NextRelease() BumpOption
NextRelease is increment to next non-prerelease version
type Version ¶
type Version struct { Major string Minor string Patch string Prerelease []string Buildmetadata []string }
func MustParse ¶
MustParse behaves like Parse but in case validation fails simply panics instead of returning an error
Example ¶
v := "1.2.3-ver.12a+build.1234" sv := semver.MustParse(v) fmt.Printf("%s == %s", v, sv.String())
Output: 1.2.3-ver.12a+build.1234 == 1.2.3-ver.12a+build.1234
func New ¶
New creates new version struct instance
Example ¶
sv, _ := semver.New( semver.SetCore("1.2.3"), semver.Prerelease("rc.1"), semver.BuildMetadata("cafebabe"), ) fmt.Printf("%s", sv.String())
Output: 1.2.3-rc.1+cafebabe
func (*Version) Bump ¶
func (semver *Version) Bump(options ...BumpOption) (Version, error)
Bump changes version to newer using provided list of bump options
Example ¶
sv := semver.MustParse("1.2.3-rc.1+cafebabe") sv, _ = sv.Bump(semver.NextMajor()) fmt.Printf("%s", sv.String())
Output: 2.0.0
func (*Version) MustBump ¶
func (semver *Version) MustBump(options ...BumpOption) Version
MustBump works like Bump but panics in cases where Bump returns an error