快速设置应用版本信息,如果你需要编写多个应用的版本设置代码,你可能需要这个。
go get -u github.com/zdz1715/go-pkg-version@latest
只需要版本信息,忽略git
和buildDate
信息,则可以直接在代码里调用SetVersion()
方法设置
package main
import (
"fmt"
gopkgversion "github.com/zdz1715/go-pkg-version"
)
func main() {
gopkgversion.SetVersion("v1.22.3")
info := gopkgversion.NewVersionInfo("golang")
fmt.Println(info)
}
Output:
golang: {"major":"1","minor":"22","patch":"3","version":"v1.22.3","goVersion":"go1.22.3","compiler":"gc","platform":"windows/amd64"}
需要git
和buildDate
信息,可以在Makefile
里注入版本信息,下面使用git tag为版本号
# Git information
GIT_COMMIT = $(shell git rev-parse HEAD)
#GIT_COMMIT_HASH = $(shell git rev-parse --short HEAD)
GIT_COMMIT_HASH = $(shell git rev-parse HEAD)
GIT_TAG = $(shell git describe --tags --abbrev=0 --exact-match 2>/dev/null)
GIT_TREESTATE = $(shell test -n "`git status --porcelain`" && echo "dirty" || echo "clean")
BUILDDATE = $(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
LDFLAGS += -X github.com/zdz1715/go-pkg-version.version=$(GIT_TAG)
LDFLAGS += -X github.com/zdz1715/go-pkg-version.gitCommit=$(GIT_COMMIT_HASH)
LDFLAGS += -X github.com/zdz1715/go-pkg-version.gitTreeState=$(GIT_TREESTATE)
LDFLAGS += -X github.com/zdz1715/go-pkg-version.buildDate=$(BUILDDATE)
.PHONY: build
build: ## Build binary.
go build -ldflags "$(LDFLAGS)" -o app app/main.go
字段 | 说明 |
---|---|
name | 应用名称 |
major | 主要版本号 |
minor | 次要版本号 |
version | 版本号 |
gitCommit | Git 提交hash |
gitTreeState | Git 提交状态: 'clean' or 'dirty' |
buildDate | 构建时间 |
goVersion | go 版本 |
compiler | 编译器名称 |
platform | 系统架构,format: os/arch |