A lightweight Go package for embedding version, build, and git metadata into your application binaries. Supports multiple injection methods with automatic fallback.
Install script:
curl -sSfL https://raw.githubusercontent.com/rbaliyan/go-version/main/install.sh | shGo install:
go install github.com/rbaliyan/go-version/cmd/go-version@latestDownload binary:
Download from GitHub Releases for your platform.
go get github.com/rbaliyan/go-versionThe go-version CLI generates .version files and ldflags for CI pipelines.
go-version file # Generate a .version file
go-version ldflags # Generate -ldflags for go build
go-version show # Show git version info
go-version version # Show go-version CLI version# Generate from git info
go-version file
# Custom output path
go-version file -o build/.version
# Manual version override
go-version file -v 1.2.3
# Custom timestamp format (uses date command format strings)
go-version file --timeformat "%Y-%m-%d"# Use in build command (shell substitutions, no timestamp by default)
go build -ldflags="$(go-version ldflags)" ./cmd/myapp
# Include build timestamp (RFC 3339 by default)
go build -ldflags="$(go-version ldflags -t)" ./cmd/myapp
# Custom timestamp format
go build -ldflags="$(go-version ldflags -t --timeformat '%Y-%m-%d')" ./cmd/myapp
# Static values for CI pipelines
go build -ldflags="$(go-version ldflags --static -t)" ./cmd/myapp
# Custom package path
go build -ldflags="$(go-version ldflags -p mycompany/myapp)" ./cmd/myappgo-version showOutput:
Version: v1.0.2
Commit: f663cfdfb69bfd922a55e56e29a7784aab73e8c3
Branch: master
Repo: git@github.com:user/repo.git
Shell completions are included in the release archives:
- Bash:
completions/go-version.bash - Zsh:
completions/go-version.zsh - Fish:
completions/go-version.fish
package main
import (
"fmt"
version "github.com/rbaliyan/go-version"
)
func main() {
// Set application info (optional)
version.SetAppInfo("myapp", "My awesome application")
// Print all version info
version.Print()
// Or access individual components
fmt.Printf("Version: %d.%d.%d\n", version.Get().Major, version.Get().Minor, version.Get().Patch)
fmt.Printf("Commit: %s\n", version.Git().Commit)
}Inject version metadata at build time using -ldflags:
go build -ldflags="\
-X github.com/rbaliyan/go-version.VersionInfo=1.2.3 \
-X github.com/rbaliyan/go-version.GitCommit=$(git rev-parse HEAD) \
-X github.com/rbaliyan/go-version.GitBranch=$(git branch --show-current) \
-X github.com/rbaliyan/go-version.GitRepo=$(git remote get-url origin) \
-X 'github.com/rbaliyan/go-version.BuildTimestamp=$(date -u "+%a %b %d %H:%M:%S %Z %Y")'"Or use the CLI:
go build -ldflags="$(go-version ldflags -static)" ./cmd/myappVersion info can be loaded from (in priority order):
- ldflags - Build-time injection via
-Xflags (loaded automatically) - Setters - Runtime calls to
SetVersion(),SetGitInfo(), etc. - Version file - Call
LoadFromFile()to load from a.versionfile - Git - Call
LoadFromGit()to detect from git repository
Create a .version file (Key=Value format):
VERSION=1.2.3
GIT_COMMIT=abc123def456
GIT_BRANCH=main
GIT_REPO=github.com/user/repo
BUILD_TIMESTAMP=Mon Jan 2 15:04:05 UTC 2006
The package searches for .version in these locations (in order):
- Current working directory (
./.version) - Executable directory (
<exe_dir>/.version) - User config (
~/.config/<appname>/.version) - requiresSetAppInfo()first - System config (
/etc/<appname>/.version) - requiresSetAppInfo()first
Call LoadFromGit() to read version info from git commands:
version.LoadFromGit()This reads:
- Commit hash from
git rev-parse HEAD - Branch from
git rev-parse --abbrev-ref HEAD - Version from
git describe --tags --always - Remote URL from
git remote get-url origin
| Function | Description |
|---|---|
SetAppInfo(name, description) |
Set application name and description |
SetVersion(ver) |
Parse and set semantic version (supports v prefix and suffixes like 1.2.3-dev) |
SetBuildInfo(timestamp) |
Set build timestamp (accepts multiple formats: RFC 3339, UnixDate, RFC 1123, etc.) |
SetGitInfo(commit, branch, repo) |
Set git metadata |
SetChangelog(changelog) |
Set changelog text |
SetChangelogFromFile(path) |
Load changelog from file |
LoadFromFile(path) |
Load version info from a specific .version file |
LoadFromGit() |
Manually trigger git auto-detection |
All setters are idempotent—they only set values once and ignore subsequent calls.
| Function | Returns |
|---|---|
Get() |
Version struct with Major, Minor, Patch, Raw, Prefix fields |
Build() |
BuildInfo struct with Timestamp and Git info |
Git() |
GitInfo struct with Commit, Branch, Repo |
App() |
AppInfo struct with Name, Description, Changelog |
Print() |
Outputs all version info to stdout |
These package-level variables can be set via -ldflags -X:
VersionInfo- Version string (e.g., "1.2.3" or "v1.2.3-dev")GitCommit- Git commit hashGitBranch- Git branch nameGitRepo- Git repository URLBuildTimestamp- Build time (supports multiple formats: RFC 3339, UnixDate, RFC 1123, etc.)
MIT License - see LICENSE file.