semver

package module
v0.0.0-...-5913d1a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2024 License: BSD-3-Clause Imports: 6 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Semver

Semver is a simple, lightweight library for managing and comparing Semantic Versioning (SemVer) strings in Golang. It provides an easy-to-use API for parsing, comparing, and upgrading semantic version strings.

Features

  • Parse SemVer strings into structured Version objects
  • Compare two SemVer strings, determining their relative order
  • Check for major, minor, and patch-level upgrades between two SemVer strings
  • Convert Version objects to JSON and strings

Usage

To use Semver in your project, first import the library:

import "github.com/stevegt/semver"
Parsing

Parse a SemVer string into a Version object:

v, err := semver.Parse([]byte("v1.2.3"))
if err != nil {
    log.Fatal(err)
}
Comparing

Compare two Version objects:

cmp := semver.Cmp(version1, version2)
// cmp == 0 if version1 == version2
// cmp == 1 if version1 > version2
// cmp == -1 if version1 < version2
Upgrading

Check if a version is an upgrade (major, minor, or patch) from another version:

major, minor, patch := semver.Upgrade(version1, version2)
// major: true if v2 is a major upgrade from V1
// minor: true if v2 is a minor upgrade from V1
// patch: true if v2 is a patch upgrade from V1
Converting

Convert a Version object to a JSON string:

jsonString := version.ToJSON()

Convert a Version object to a string:

versionString := version.String()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cmp

func Cmp(v1, v2 *Version) (int, error)

Cmp takes two version structs and returns an integer value indicating the relationship between the two versions. 0: v1 == v2 1: v1 > v2 -1: v1 < v2

func CmpPart

func CmpPart(v1, v2 string) (cmp int, err error)

CmpPart takes two version components (e.g. Major, Minor, Patch, Suffix) and returns an integer value indicating the relationship between the two components. 0: v1 == v2 1: v1 > v2 -1: v1 < v2

func Upgrade

func Upgrade(v1, v2 *Version) (major, minor, patch, suffix bool, err error)

Upgrade takes two version structs and returns three bools indicating the relationship between the two versions. 1. major: true if v2 is a major upgrade from V1 2. minor: true if v2 is a minor upgrade from V1 3. patch: true if v2 is a patch upgrade from V1 4. suffix: true if v2 is a suffix upgrade from V1

Types

type Version

type Version struct {
	// Major version number (required)
	Major string `json:"major"`
	// Minor version number (required)
	Minor string `json:"minor"`
	// Patch level (optional)
	Patch string `json:"patch,omitempty"`
	// Suffix string, (optional)
	Suffix string `json:"suffix,omitempty"`
}

Version holds the information to generate a semver string

func Parse

func Parse(src []byte) (v *Version, err error)

Parse takes a byte slice and returns a version struct, and an error value.

func (*Version) String

func (v *Version) String() string

func (*Version) ToJSON

func (v *Version) ToJSON() []byte

ToJSON takes a version struct and returns JSON as byte slice

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址