Documentation
¶
Overview ¶
Package version exposes a binary's build metadata (version, commit, build date, Go version) in a single, dependency-free place that can be shared across every binary in an organisation.
Values are injected at build time via the Go linker's -X flag and fall back to development placeholders for `go run` / `go build` invocations that don't stamp them — so an unstamped build correctly reports itself as "dev".
Inject the values by targeting the exported package variables below. Because `-X` can write to any package linked into the binary, every consuming binary stamps these same variables at its own build step, each with its own values:
go build -ldflags "\ -X github.com/automa-saga/version.Version=v1.2.3 \ -X github.com/automa-saga/version.Commit=$(git rev-parse HEAD) \ -X github.com/automa-saga/version.Date=$(date -u +%Y-%m-%dT%H:%M:%SZ)"
Read the running binary's metadata through Get:
info := version.Get() fmt.Println(info.Text())
The package intentionally has zero third-party dependencies. It renders text and JSON using only the standard library; any other format (e.g. YAML) is left to the caller, which can marshal the exported Info fields directly.
Index ¶
Constants ¶
const ( FormatJSON = "json" FormatText = "text" )
Output formats supported by Format.
Variables ¶
var ( // Version is the semantic version (e.g. v1.2.3), or "dev" for unstamped builds. Version = "dev" // Commit is the git commit hash the binary was built from, or "none". Commit = "none" // Date is the build timestamp (RFC 3339), or "unknown". Date = "unknown" )
Build metadata, set via -ldflags "-X github.com/automa-saga/version.<var>=...". The defaults are deliberate, human-readable markers for unstamped builds.
Functions ¶
This section is empty.
Types ¶
type Info ¶
type Info struct {
Version string `json:"version" yaml:"version"`
Commit string `json:"commit" yaml:"commit"`
Date string `json:"date,omitempty" yaml:"date,omitempty"`
GoVersion string `json:"goVersion" yaml:"goVersion"`
}
Info is a snapshot of the running binary's build metadata.
func Get ¶
func Get() Info
Get returns a snapshot of the running binary's build metadata, including the Go runtime version it was compiled with.
func (Info) Format ¶
Format renders the Info in the requested format. An empty format defaults to JSON. Only "json" and "text" are supported by this package; for other formats (e.g. YAML) marshal the Info fields directly with your own dependency.
func (Info) ShortCommit ¶
ShortCommit returns the commit hash truncated to n characters (useful for compact display). It returns the full commit if it is shorter than n.