buildinfo

package
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package buildinfo 在运行时暴露构建信息(版本 / commit / 构建时间 / Go 版本 / 模块 / 是否 dirty),用于 /version 端点、启动日志、诊断。零依赖(仅标准库)。

两个来源自动合并:

  • ldflags 注入的包级变量(优先): go build -ldflags "-X github.com/rushteam/beauty/pkg/buildinfo.version=1.2.3 \ -X github.com/rushteam/beauty/pkg/buildinfo.commit=$(git rev-parse HEAD) \ -X github.com/rushteam/beauty/pkg/buildinfo.buildTime=$(date -u +%FT%TZ)"
  • runtime/debug.ReadBuildInfo(回退):从 VCS 元数据取 revision/time/modified, 以及主模块路径与版本(go build 在 VCS 检出中会自动嵌入 vcs.*)。

未注入 ldflags 且非 VCS 构建时,尽力给出可用信息,Version 兜底 "unknown"。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler() http.HandlerFunc

Handler 返回一个把构建信息以 JSON 输出的 http.Handler,可挂到 /version。

Types

type Info

type Info struct {
	Version   string `json:"version"`
	Commit    string `json:"commit"`
	BuildTime string `json:"build_time"`
	GoVersion string `json:"go_version"`
	Module    string `json:"module"`
	Dirty     bool   `json:"dirty"` // 构建时工作区有未提交改动(vcs.modified)
}

Info 是聚合后的构建信息。

func Get

func Get() Info

Get 读取并合并构建信息(ldflags 优先,缺失项用 runtime/debug 回退)。

func (Info) Short

func (i Info) Short() string

Short 返回短 commit(前 12 位),便于日志展示。

func (Info) String

func (i Info) String() string

String 返回单行摘要,适合启动日志。

Jump to

Keyboard shortcuts

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