version

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: MIT Imports: 8 Imported by: 0

README

go-version

This package was originally extracted from sigs.k8s.io/release-utils.

Since then, it has been changed quite a bit (see full list below).

Original license can be found in NOTICE.md.

Changes

Full list of changes from the original library:

  • drop all dependencies:
    • use std testing only
    • allow to pass a previously generated ASCII art instead of generating it at runtime
  • optional overrides:
    • caller can pass one or more functions that change the version info, so callers are free to use whatever methods they want to provide some options
    • a range of functions are provided by the library
  • added more fields:
    • URL
    • BuiltBy
  • testing
    • added more tests, hopefully preventing breaking changes in the future

Documentation

Overview

Package version provides utilities to get the Go module version information. This package also allows us to inject build time variables such as commit hash. When we run this in Docker in Kubernetes, we want to be able to introspectively identify what source code generated the runtime we encounter. See https://github.com/StevenACoffman/small for more information Also https://blog.alexellis.io/inject-build-time-vars-golang/ These variables may be injected at build time in the Docker container Please see the README.md for more information as to how this is done here.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Info

type Info struct {
	GitVersion   string `json:"gitVersion"`
	ModuleSum    string `json:"moduleChecksum"`
	GitCommit    string `json:"gitCommit"`
	GitTreeState string `json:"gitTreeState"`
	BuildDate    string `json:"buildDate"`
	BuiltBy      string `json:"builtBy"`
	GoVersion    string `json:"goVersion"`
	Compiler     string `json:"compiler"`
	Platform     string `json:"platform"`

	ASCIIName   string `json:"-"`
	Name        string `json:"-"`
	Description string `json:"-"`
	URL         string `json:"-"`
}

Info provides the version info.

func GetVersionInfo

func GetVersionInfo(options ...Option) *Info

GetVersionInfo returns build information for the running binary.

func GetVersionInfoFrom

func GetVersionInfoFrom(bi *debug.BuildInfo, linkOverride string, options ...Option) *Info

GetVersionInfoFrom builds an Info from an explicit BuildInfo value. Passing nil returns an Info with all VCS fields set to "unknown" and current runtime values for GoVersion, Compiler, and Platform. This is useful for testing without touching global state.

func (*Info) JSONString

func (i *Info) JSONString() (string, error)

JSONString returns the JSON representation of the version info.

func (*Info) String

func (i *Info) String() string

String returns the string representation of the version info.

type Option

type Option func(i *Info)

Option can be used to customize the Info after it is gathered from the environment.

func WithASCIIName

func WithASCIIName(name string) Option

WithASCIIName allows you to add an ASCII art of the name.

func WithAppDetails

func WithAppDetails(name, description, url string) Option

WithAppDetails allows to set the app name and description.

func WithBuiltBy

func WithBuiltBy(name string) Option

WithBuiltBy allows to set the builder name/builder system name.

Jump to

Keyboard shortcuts

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