buildinfo

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 6 Imported by: ? modules, 0 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.

Documentation

Overview

Package buildinfo reports which build of a command is running.

Release builds stamp the version, commit and build date through linker flags. With GoReleaser:

builds:
  - ldflags:
      - -s -w
      - -X github.com/mittwald/cli-kit/buildinfo.Version={{ .Version }}
      - -X github.com/mittwald/cli-kit/buildinfo.Commit={{ .FullCommit }}
      - -X github.com/mittwald/cli-kit/buildinfo.Date={{ .Date }}

and with a plain go build:

go build -ldflags "-X github.com/mittwald/cli-kit/buildinfo.Version=1.2.3 \
  -X github.com/mittwald/cli-kit/buildinfo.Commit=$(git rev-parse HEAD)"

Builds without linker flags fall back to what the Go toolchain records in the binary: the module version for "go install example.com/tool@v1.2.3", the version Go 1.24 and later derive from the repository's tags for a plain "go build" of a tagged commit, and the VCS revision, commit time and dirty state. Any other build is a development build: Info.String reports it as "dev" with its revision, such as "dev+0123456789ab.dirty", rather than the pseudo-version the toolchain made up for it.

Index

Examples

Constants

View Source
const DevVersion = "dev"

DevVersion is the version a build without any version information reports.

Variables

View Source
var (
	// Version is the release version, with or without a leading "v".
	Version string
	// Commit is the full VCS revision the binary was built from.
	Commit string
	// Date is the build or commit time in RFC 3339 format.
	Date string
)

Values stamped at link time with -ldflags "-X …". Leave them empty in the source; Get falls back to the toolchain's build information for every value that was not stamped.

Functions

This section is empty.

Types

type Info

type Info struct {
	// Version is the release version as stamped or recorded, such as "v1.2.3"
	// or "1.2.3", or [DevVersion] for a build that is no release. It keeps
	// the version as it was stamped, a leading "v" included; [Info.String]
	// is the form shown to users.
	Version string
	// Commit is the VCS revision, or "" when unknown. It is the full
	// revision, except for a build installed by commit ("go install
	// example.com/tool@main"), which only records its first 12 characters.
	Commit string
	// Date is the build or commit time, or the zero time when unknown.
	Date time.Time
	// Modified reports whether the working tree had uncommitted changes when
	// the binary was built. Only the toolchain records it.
	Modified bool
	// GoVersion is the Go release the binary was built with.
	GoVersion string
}

Info describes one build of a command.

func Get

func Get() Info

Get returns the build information of the running binary: the values stamped through linker flags, completed from the toolchain's records.

A version stamped through linker flags is kept as it is, unless it is DevVersion. Otherwise the module version the toolchain recorded is used when it names a release: "go install example.com/tool@v1.2.3", or a plain "go build" (Go 1.24 and later) of a checkout whose commit carries the release tag and has no uncommitted changes. A version the toolchain made up for any other commit (a pseudo-version, such as "v1.2.4-0.20260929160445-2f9134944bc2"), and one it marked "+dirty", name no release: the build reports DevVersion instead, and Info.String shows it with its revision, such as "dev+2f9134944bc2.dirty".

func (Info) Detail

func (i Info) Detail(name string) string

Detail returns a multi-line description for a version command: the version line, then the revision, build date and Go release where known. name is the command's name, written in front of the version.

Example
package main

import (
	"fmt"
	"time"

	"github.com/mittwald/cli-kit/buildinfo"
)

func main() {
	info := buildinfo.Info{
		Version:   "v1.4.0",
		Commit:    "0123456789abcdef0123456789abcdef01234567",
		Date:      time.Date(2026, 11, 10, 8, 0, 0, 0, time.UTC),
		GoVersion: "go1.24.9",
	}
	fmt.Print(info.Detail("example"))
}
Output:
example 1.4.0
commit: 0123456789abcdef0123456789abcdef01234567
built:  2026-11-10T08:00:00Z
go:     go1.24.9

func (Info) ShortCommit

func (i Info) ShortCommit() string

ShortCommit returns the first 12 characters of the revision, or "" when it is unknown.

func (Info) String

func (i Info) String() string

String returns the version as shown to users. A version with a leading "v" before a digit loses it ("v1.4.0" becomes "1.4.0"; Version keeps it); a development build carries its short revision and, when the tree was dirty, a ".dirty" marker ("dev+0123456789ab.dirty").

Example
package main

import (
	"fmt"

	"github.com/mittwald/cli-kit/buildinfo"
)

func main() {
	release := buildinfo.Info{Version: "v1.4.0"}
	dev := buildinfo.Info{Version: buildinfo.DevVersion, Commit: "0123456789abcdef0123", Modified: true}
	fmt.Println(release)
	fmt.Println(dev)
}
Output:
1.4.0
dev+0123456789ab.dirty

Jump to

Keyboard shortcuts

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