version

package
v0.0.0-...-49bf833 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2023 License: MIT Imports: 4 Imported by: 0

README

版本功能

  • 通过go build时的-ldflags参数,将版本信息注入到二进制文件中

版本信息输出示例:

gitVersion: 9440e49                                 
gitCommit: 9440e4978a24f7151c0e39e6aec40d04ed8965ca
gitTreeState: clean                                   
buildDate: 2023-09-11T11:10:22Z                    
goVersion: go1.21.1                                
compiler: gc                                      
platform: linux/amd64

实现细节

  • 在 Makefile 中通过 go build -ldflags $(GO_LDFLAGS) 获取部分版本信息
    • 使用 git describe --tags --always --match='v*' 获取版本号
    • 使用 date -u +'%Y-%m-%dT%H:%M:%SZ' 获取构建时间
    • 使用 git rev-parse HEAD 获取构建是 commit ID
    • 通过 git status --porcelain 2 获取 git tree state
  • 使用标准库 runtime 获取部分信息
    • 使用 runtime.Version() 获取 Go 版本信息
    • 使用 runtime.Compiler 获取编译器信息
    • 使用 fmt.Sprintf( "%s/%s" , runtime.GOOS, runtime.GOARCH) 获取编译环境信息

--version 选项实现

  • 调用 pflag 库将 --version 选项添加到 flag 中
  • verflag.AddFlags(cmd.PersistentFlags())--version 标志持久化,使其可以在子命令中使用

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// GitVersion is a semantic version number
	GitVersion = "v0.0.0-master+$Format:%h$"
	// BuildDate format is ISO8601
	BuildDate = "1970-01-01T00:00:00Z"
	// GitCommit is the commit hash
	GitCommit = "$Format:%H$"
	// GitTreeState is the state of Git repository during the build
	// 'clean' means  no uncommitted changes
	// 'dirty' means there are uncommitted changes
	GitTreeState = ""
)

Functions

This section is empty.

Types

type Info

type Info struct {
	GitVersion   string `json:"gitVersion"`
	GitCommit    string `json:"gitCommit"`
	GitTreeState string `json:"gitTreeState"`
	BuildDate    string `json:"buildDate"`
	GoVersion    string `json:"goVersion"`
	Compiler     string `json:"compiler"`
	Platform     string `json:"platform"`
}

Info include the version info

func Get

func Get() Info

Get return comprehensive version info

func (Info) String

func (info Info) String() string

String returns the readable version info

func (Info) Text

func (info Info) Text() ([]byte, error)

Text encodes version info in UTF-8 formatted text and return it

func (Info) ToJson

func (info Info) ToJson() string

ToJson returns the json format version info

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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