version

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2025 License: BSD-3-Clause Imports: 7 Imported by: 1

Documentation

Overview

Package version provides utilities to manage, retrieve, and validate version information of a Go application at runtime, supporting multiple version formats including Semantic Versioning (SemVer) and Calendar Versioning (CalVer).

This package captures key metadata about the application build, including the Git tag, full and short commit hashes, build date, Go runtime version, target platform, and the list of Go modules used in the build. These values can be set at build time via linker flags (-ldflags), or automatically extracted from debug.BuildInfo at runtime when available.

Version Information Sources:

1. Build-time ldflags (recommended for releases):

  • Provides precise control over version information
  • Set via -ldflags during go build

2. Runtime debug.BuildInfo (automatic fallback):

  • Automatically used when ldflags are not set
  • Extracts vcs.revision (git commit), vcs.time (build date), vcs.modified
  • Available when building with Go 1.18+ and VCS information embedded
  • Can be manually triggered via LoadFromBuildInfo()

Supported Version Formats:

1. Semantic Versioning (SemVer): vX.Y.Z format

  • Example: v1.2.3, v2.0.0-alpha
  • Follows semantic versioning specification

2. Calendar Versioning (CalVer): Various formats based on dates

  • YYYY.MM.DD: v2024.10.02
  • YY.MM.MICRO: v24.10.123
  • YYYY.WW: v2024.42 (year and week number)
  • YYYY.MM.DD.MICRO: v2024.10.02.456

The package automatically detects the version format and provides appropriate parsing, validation, and comparison functions. It includes functions to parse version components, validate version structs, compare versions, and extract semantic or calendar-based information from Git tags.

The module list is automatically populated at runtime using debug.BuildInfo (available since Go 1.12+), which extracts module dependencies embedded by the Go build system.

Typical usage involves setting the version variables during build with `-ldflags`, for example in a Makefile:

GIT_TAG := $(shell git describe --tags)
GIT_COMMIT := $(shell git rev-parse HEAD)
GIT_SHORT := $(shell git rev-parse --short HEAD)
BUILD_TIME := $(shell date +%FT%T%z)
VERSION_PACKAGE := github.com/valentin-kaiser/go-core/version

go build -ldflags "-X $(VERSION_PACKAGE).GitTag=$(GIT_TAG) \
                 -X $(VERSION_PACKAGE).GitCommit=$(GIT_COMMIT) \
                 -X $(VERSION_PACKAGE).GitShort=$(GIT_SHORT) \
                 -X $(VERSION_PACKAGE).BuildDate=$(BUILD_TIME)" main.go

The package defines the Release struct encapsulating all relevant fields, the ParsedVersion struct for version components, and the VersionParser interface for extensible version format support.

Example usage with different version formats:

// Get current version information
v := version.GetVersion()
fmt.Printf("App version: %s (commit %s)\n", v.GitTag, v.GitShort)
fmt.Printf("Format: %s\n", v.VersionFormat.String())

// Parse any supported version format
parsed, err := version.ParseVersion("v2024.10.02")
if err == nil {
	fmt.Printf("Year: %d, Month: %d, Day: %d\n", parsed.Year, parsed.Month, parsed.Day)
}

// Compare versions of the same format
result, err := version.CompareVersions("v1.2.3", "v1.2.4")
if err == nil && result < 0 {
	fmt.Println("First version is older")
}

Index

Constants

This section is empty.

Variables

View Source
var (
	// GitTag is the release tag of the application, typically in the format "vX.Y.Z".
	GitTag = "v0.0.0"
	// GitCommit is the full commit hash of the application at build time.
	GitCommit = "unknown"
	// GitShort is the short commit hash of the application at build time.
	GitShort = "unknown"
	// BuildDate is the date and time when the application was built.
	BuildDate = "unknown"
	// GoVersion is the version of the Go runtime used to build the application.
	GoVersion = runtime.Version()
	// Platform is the target platform of the application, formatted as "GOOS/GOARCH".
	Platform = runtime.GOOS + "/" + runtime.GOARCH
	// Modules is a list of Go modules used in the application, populated from debug.BuildInfo.
	Modules = make([]*Module, 0)
)

Functions

func CompareVersions

func CompareVersions(tag1, tag2 string) (int, error)

CompareVersions compares two version strings, returns -1, 0, or 1

func ExtractSemanticVersion

func ExtractSemanticVersion(tag string) string

ExtractSemanticVersion parses the version number from the Git tag. It returns the version string without the "v" prefix and any pre-release/build metadata. If the tag is not a valid Git tag, it returns an empty string.

func GetVersionComponents

func GetVersionComponents(tag string) (map[string]interface{}, error)

GetVersionComponents returns the version components as a map for any supported format

func IsCalVerYYMMMICRO

func IsCalVerYYMMMICRO(tag string) bool

IsCalVerYYMMMICRO checks if the tag is in YY.MM.MICRO CalVer format

func IsCalVerYYYYMMDD

func IsCalVerYYYYMMDD(tag string) bool

IsCalVerYYYYMMDD checks if the tag is in YYYY.MM.DD CalVer format

func IsCalVerYYYYMMDDMICRO

func IsCalVerYYYYMMDDMICRO(tag string) bool

IsCalVerYYYYMMDDMICRO checks if the tag is in YYYY.MM.DD.MICRO CalVer format

func IsCalVerYYYYWW

func IsCalVerYYYYWW(tag string) bool

IsCalVerYYYYWW checks if the tag is in YYYY.WW CalVer format

func IsSemver

func IsSemver(tag string) bool

IsSemver checks if the provided tag is a valid Git tag in semantic versioning format "vX.Y.Z".

func IsValidVersion

func IsValidVersion(tag string) bool

IsValidVersion checks if a version string is valid in any supported format

func Major

func Major() int

Major returns the major version number from the Git tag if it follows semantic versioning.

func Minor

func Minor() int

Minor returns the minor version number from the Git tag if it follows semantic versioning.

func ParseSemver

func ParseSemver(tag string, n int) int

ParseSemver parses the specified segment (major, minor, or patch) from the Git tag.

func Patch

func Patch() int

Patch returns the patch version number from the Git tag if it follows semantic versioning.

func String

func String() string

String returns the version tag as a string without the "v" prefix.

Types

type CalVerYYMMMICROParser

type CalVerYYMMMICROParser struct{}

CalVerYYMMMICROParser implements VersionParser for YY.MM.MICRO CalVer format

func (*CalVerYYMMMICROParser) Compare

func (p *CalVerYYMMMICROParser) Compare(tag1, tag2 string) (int, error)

Compare compares two YY.MM.MICRO calendar version tags and returns -1, 0, or 1.

func (*CalVerYYMMMICROParser) Format

func (p *CalVerYYMMMICROParser) Format() Format

Format returns the version format type handled by this parser.

func (*CalVerYYMMMICROParser) IsValid

func (p *CalVerYYMMMICROParser) IsValid(tag string) bool

IsValid checks if the given tag is a valid YY.MM.MICRO calendar version format.

func (*CalVerYYMMMICROParser) Parse

func (p *CalVerYYMMMICROParser) Parse(tag string) (*ParsedVersion, error)

Parse parses a YY.MM.MICRO calendar version tag and returns a ParsedVersion struct with year, month, and micro components.

type CalVerYYYYMMDDMICROParser

type CalVerYYYYMMDDMICROParser struct{}

CalVerYYYYMMDDMICROParser implements VersionParser for YYYY.MM.DD.MICRO CalVer format

func (*CalVerYYYYMMDDMICROParser) Compare

func (p *CalVerYYYYMMDDMICROParser) Compare(tag1, tag2 string) (int, error)

Compare compares two YYYY.MM.DD.MICRO calendar version tags and returns -1, 0, or 1.

func (*CalVerYYYYMMDDMICROParser) Format

func (p *CalVerYYYYMMDDMICROParser) Format() Format

Format returns the version format type handled by this parser.

func (*CalVerYYYYMMDDMICROParser) IsValid

func (p *CalVerYYYYMMDDMICROParser) IsValid(tag string) bool

IsValid checks if the given tag is a valid YYYY.MM.DD.MICRO calendar version format.

func (*CalVerYYYYMMDDMICROParser) Parse

Parse parses a YYYY.MM.DD.MICRO calendar version tag and returns a ParsedVersion struct with year, month, day, and micro components.

type CalVerYYYYMMDDParser

type CalVerYYYYMMDDParser struct{}

CalVerYYYYMMDDParser implements VersionParser for YYYY.MM.DD CalVer format

func (*CalVerYYYYMMDDParser) Compare

func (p *CalVerYYYYMMDDParser) Compare(tag1, tag2 string) (int, error)

Compare compares two YYYY.MM.DD calendar version tags and returns -1, 0, or 1.

func (*CalVerYYYYMMDDParser) Format

func (p *CalVerYYYYMMDDParser) Format() Format

Format returns the version format type handled by this parser.

func (*CalVerYYYYMMDDParser) IsValid

func (p *CalVerYYYYMMDDParser) IsValid(tag string) bool

IsValid checks if the given tag is a valid YYYY.MM.DD calendar version format.

func (*CalVerYYYYMMDDParser) Parse

func (p *CalVerYYYYMMDDParser) Parse(tag string) (*ParsedVersion, error)

Parse parses a YYYY.MM.DD calendar version tag and returns a ParsedVersion struct with year, month, and day components.

type CalVerYYYYWWParser

type CalVerYYYYWWParser struct{}

CalVerYYYYWWParser implements VersionParser for YYYY.WW CalVer format

func (*CalVerYYYYWWParser) Compare

func (p *CalVerYYYYWWParser) Compare(tag1, tag2 string) (int, error)

Compare compares two YYYY.WW calendar version tags and returns -1, 0, or 1.

func (*CalVerYYYYWWParser) Format

func (p *CalVerYYYYWWParser) Format() Format

Format returns the version format type handled by this parser.

func (*CalVerYYYYWWParser) IsValid

func (p *CalVerYYYYWWParser) IsValid(tag string) bool

IsValid checks if the given tag is a valid YYYY.WW calendar version format.

func (*CalVerYYYYWWParser) Parse

func (p *CalVerYYYYWWParser) Parse(tag string) (*ParsedVersion, error)

Parse parses a YYYY.WW calendar version tag and returns a ParsedVersion struct with year and week components.

type Format

type Format int

Format represents the type of version format

const (
	// FormatUnknown represents an unknown or unsupported version format
	FormatUnknown Format = iota
	// FormatSemVer represents semantic versioning (vX.Y.Z)
	FormatSemVer
	// FormatCalVerYYYYMMDD represents calendar versioning YYYY.MM.DD
	FormatCalVerYYYYMMDD
	// FormatCalVerYYMMMICRO represents calendar versioning YY.MM.MICRO
	FormatCalVerYYMMMICRO
	// FormatCalVerYYYYWW represents calendar versioning YYYY.WW
	FormatCalVerYYYYWW
	// FormatCalVerYYYYMMDDMICRO represents calendar versioning YYYY.MM.DD.MICRO
	FormatCalVerYYYYMMDDMICRO
)

func DetectFormat

func DetectFormat(tag string) Format

DetectFormat automatically detects the version format from a tag string

func (Format) String

func (f Format) String() string

String returns the string representation of the version format

type Module

type Module struct {
	Path    string
	Version string
	Sum     string
	Replace *Module `json:",omitempty"`
}

Module represents a module dependency of the application. It includes the module path, version, checksum, and an optional replacement module.

type ParsedVersion

type ParsedVersion struct {
	Original string                 `json:"original"`
	Format   Format                 `json:"format"`
	Major    int                    `json:"major,omitempty"`
	Minor    int                    `json:"minor,omitempty"`
	Patch    int                    `json:"patch,omitempty"`
	Micro    int                    `json:"micro,omitempty"`
	Year     int                    `json:"year,omitempty"`
	Month    int                    `json:"month,omitempty"`
	Day      int                    `json:"day,omitempty"`
	Week     int                    `json:"week,omitempty"`
	Extra    map[string]interface{} `json:"extra,omitempty"`
}

ParsedVersion represents the parsed components of a version string

func ParseVersion

func ParseVersion(tag string) (*ParsedVersion, error)

ParseVersion parses a version string and returns a ParsedVersion struct

func (*ParsedVersion) String

func (pv *ParsedVersion) String() string

String returns the version string without the "v" prefix

type Parser

type Parser interface {
	// Parse extracts version components from a version string
	Parse(tag string) (*ParsedVersion, error)
	// IsValid checks if a version string is valid for this format
	IsValid(tag string) bool
	// Format returns the version format type
	Format() Format
	// Compare compares two version strings, returns -1, 0, or 1
	Compare(tag1, tag2 string) (int, error)
}

Parser defines the interface for parsing and validating version strings

func GetParser

func GetParser(format Format) Parser

GetParser returns the appropriate parser for the given format

type Release

type Release struct {
	ID            uint64         `json:"-" gorm:"primaryKey"`
	GitTag        string         `json:"gitTag" gorm:"uniqueIndex:idx_version_module"`
	GitCommit     string         `json:"gitCommit" gorm:"uniqueIndex:idx_version_module"`
	GitShort      string         `json:"gitShort"`
	BuildDate     string         `json:"buildDate" gorm:"uniqueIndex:idx_version_module"`
	GoVersion     string         `json:"goVersion" gorm:"uniqueIndex:idx_version_module"`
	Platform      string         `json:"platform" gorm:"uniqueIndex:idx_version_module"`
	Modules       []*Module      `json:"modules" gorm:"-"`
	VersionFormat Format         `json:"versionFormat" gorm:"-"`
	ParsedVersion *ParsedVersion `json:"parsedVersion" gorm:"-"`
}

Release represents the version information of the application. It includes the Git tag, commit hash, build date, Go version, platform, and a list of modules.

func Get

func Get() *Release

Get returns the current version information of the application.

func (*Release) Compare

func (v *Release) Compare(c *Release) bool

Compare compares the Git tag and commit hash of the current version with another version.

func (*Release) CompareCommit

func (v *Release) CompareCommit(c *Release) bool

CompareCommit compares the Git commit hash and short hash of the current version with another version.

func (*Release) CompareTag

func (v *Release) CompareTag(c *Release) bool

CompareTag compares the Git tag of the current version with another version.

func (*Release) CompareVersions

func (v *Release) CompareVersions(c *Release) (int, error)

CompareVersions compares the version of the current release with another release using proper version comparison

func (*Release) GetVersionComponents

func (v *Release) GetVersionComponents() (map[string]interface{}, error)

GetVersionComponents returns the parsed version components

func (*Release) IsCalVer

func (v *Release) IsCalVer() bool

IsCalVer returns true if the release uses Calendar Versioning

func (*Release) IsSemVer

func (v *Release) IsSemVer() bool

IsSemVer returns true if the release uses Semantic Versioning

func (*Release) Validate

func (v *Release) Validate(change *Release) error

Validate checks if the provided version information is valid.

type SemVerParser

type SemVerParser struct{}

SemVerParser implements VersionParser for semantic versioning

func (*SemVerParser) Compare

func (p *SemVerParser) Compare(tag1, tag2 string) (int, error)

Compare compares two semantic version tags and returns -1, 0, or 1.

func (*SemVerParser) Format

func (p *SemVerParser) Format() Format

Format returns the version format type handled by this parser.

func (*SemVerParser) IsValid

func (p *SemVerParser) IsValid(tag string) bool

IsValid checks if the given tag is a valid semantic version format.

func (*SemVerParser) Parse

func (p *SemVerParser) Parse(tag string) (*ParsedVersion, error)

Parse parses a semantic version tag and returns a ParsedVersion struct with major, minor, and patch components.

Jump to

Keyboard shortcuts

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