version

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2023 License: MIT Imports: 4 Imported by: 5

README

version

Dealing with versions.

Usage

see example

Documentation

Overview

Example
package main

import (
	"fmt"

	"gitlab.com/golang-utils/version"
)

func main() {

	var v1 = "v1.1.0"
	var v2 = "1.0.4"
	var invalid = ".1.0"

	vers1, err := version.Parse(v1)

	if err != nil {
		fmt.Printf("could not parse version %q", v1)
	}

	vers2, err := version.Parse(v2)

	if err != nil {
		fmt.Printf("could not parse version %q", v1)
	}

	if vers2.Less(*vers1) {
		fmt.Printf("version %s < version %s\n", vers2, vers1)
	}

	vers2.Minor = 1

	if vers1.Less(*vers2) {
		fmt.Printf("version %s < version %s\n", vers1, vers2)
	}

	last := version.Versions{vers1, vers2}.Sort().Last()

	fmt.Printf("last version is %s\n", last)

	_, err = version.Parse(invalid)

	if err != nil {
		fmt.Printf("could not parse version %q", invalid)
	}

}
Output:

version 1.0.4 < version 1.1.0
version 1.1.0 < version 1.1.4
last version is 1.1.4
could not parse version ".1.0"

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Version

type Version struct {
	Major uint16
	Minor uint16
	Patch uint16
}

Version represents a version (e.g. a release version)

func Parse

func Parse(v string) (*Version, error)

Parse parses the version out of the given string. Valid strings are "v0.0.1" or "1.0" or "12" etc.

func (Version) Equals

func (v Version) Equals(o Version) bool

Equals is true when the version exactly equals the given version

func (Version) EqualsMajor

func (v Version) EqualsMajor(o Version) bool

EqualsMajor is true when the version equals the given version on the Major

func (Version) EqualsMinor

func (v Version) EqualsMinor(o Version) bool

EqualsMinor is true when the version equals the given version on the Major and Minor

func (Version) Less

func (v Version) Less(o Version) bool

Less returns true, if the Version is smaller than the given one.

func (Version) String

func (v Version) String() string

String returns the string representation of the version (without a preceeding v

type Versions

type Versions []*Version

Versions is a sortable slice of *Version

func (Versions) First

func (v Versions) First() *Version

First returns the first *Version of the slice.

func (Versions) Last

func (v Versions) Last() *Version

Last returns the last *Version of the slice.

func (Versions) Len

func (v Versions) Len() int

Len returns the number of *Version inside the slice

func (Versions) Less

func (v Versions) Less(a, b int) bool

Less returns true, if the version of index a is less than the version of index b

func (Versions) Sort

func (v Versions) Sort() Versions

Sort sorts the slice and returns it

func (Versions) Swap

func (v Versions) Swap(a, b int)

Swap swaps the *Version of the index a with that of the index b

Jump to

Keyboard shortcuts

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