版本信息
构建时与运行时版本、ldflags 注入
在线 Playground
在浏览器中直接体验本页相关 API,无需本地安装 Go 环境。
version
Build-time and runtime version information for the ling-base library.
Variables
| Variable | Default | Description |
|---|---|---|
Version | "1.0.0" | Semantic version string |
GitCommit | "unknown" | Short Git commit hash |
BuildTime | "unknown" | Build timestamp (RFC3339) |
GoVersion | "unknown" | Go toolchain version |
Functions
GetVersion()— returns the version stringGetVersionInfo()— returns a human-readable summaryGetGitCommit()— returns the Git commit hashGetBuildTime()— returns the build timestampGetGoVersion()— returns the Go version
Link-time override
Override at build time with -ldflags:
go build -ldflags "\
-X github.com/LingByte/ling-base/version.Version=1.2.0 \
-X github.com/LingByte/ling-base/version.GitCommit=$(git rev-parse --short HEAD) \
-X github.com/LingByte/ling-base/version.BuildTime=$(date -u +%Y-%m-%dT%H:%M:%SZ) \
-X github.com/LingByte/ling-base/version.GoVersion=$(go version)" \
./...Automatic resolution
When not overridden via -ldflags, GitCommit and BuildTime are resolved
automatically from:
- VCS build info (Go 1.18+
debug.ReadBuildInfo) - The local git repository (via
gitcommand)
Usage
import "github.com/LingByte/ling-base/version"
fmt.Println(version.GetVersionInfo())
// Output: 1.0.0 (commit: abc123def456, built at: 2026-01-15T10:30:00Z, go: go1.26.2)License
MIT