使用 Go 管理版本的方法示例

 更新时间:2019年10月31日 10:17:30   作者:帅气猫咪  
这篇文章主要介绍了使用 Go 管理版本的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

简介

如果你曾经运行过 docker version,

就会发现它提供了很多信息:

PS C:\Users\tzh> docker version
Client: Docker Engine - Community
 Version:      19.03.4
 API version:    1.40
 Go version:    go1.12.10
 Git commit:    9013bf5
 Built:       Thu Oct 17 23:44:48 2019
 OS/Arch:      windows/amd64
 Experimental:   false

Server: Docker Engine - Community
 Engine:
 Version:     19.03.4
 API version:   1.40 (minimum version 1.12)
 Go version:    go1.12.10
 Git commit:    9013bf5
 Built:      Thu Oct 17 23:50:38 2019
 OS/Arch:     linux/amd64
 Experimental:   false
 containerd:
 Version:     v1.2.10
 GitCommit:    b34a5c8af56e510852c35414db4c1f4fa6172339
 runc:
 Version:     1.0.0-rc8+dev
 GitCommit:    3e425f80a8c931f88e6d94a8c831b9d5aa481657
 docker-init:
 Version:     0.18.0
 GitCommit:    fec3683

对于编译好的二进制文件而言, 获取版本信息是非常重要的.
尽可能地提供详细信息, 有利于后期的维护和排错.

如何实现

对于版本信息等, 有两种方式,

一种从外部获取, 比如配置文件等,

另一种从源代码中获取, 将配置信息写死在源代码中.

这两种都不太好, 比如编译时间就不太好确定.
最好是能在 go build 时确定这些信息.

幸好, go build 提供了一个选项叫做 -ldflags '[pattern=]arg list'.

-X importpath.name=value
  Set the value of the string variable in importpath named name to value.
  This is only effective if the variable is declared in the source code either uninitialized
  or initialized to a constant string expression. -X will not work if the initializer makes
  a function call or refers to other variables.
  Note that before Go 1.5 this option took two separate arguments.

这使得我们可以在编译生成二进制文件时, 指定某些变量的值.

比如我们有个文件是 company/buildinfo 包的一部分.

package buildinfo

var BuildTime string

运行 go build -ldflags="-X 'company/buildinfo.BuildTime=$(date)'" 会记录编译时间,

将 BuildTime 的值设置为编译时的时间, 即从 $(date) 中获取的时间.

参考:

Compile packages and dependencies
Command link
Including build information in the executable

实践

新增 pkg/version 包, 用于获取版本信息.

package version

// 这些值应该是从外部传入的
var (
  gitTag    string = ""
  gitCommit  string = "$Format:%H$"     // sha1 from git, output of $(git rev-parse HEAD)
  gitTreeState string = "not a git tree"    // state of git tree, either "clean" or "dirty"
  buildDate  string = "1970-01-01T00:00:00Z" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
)

package version

import (
  "fmt"
  "runtime"
)

// 构建时的版本信息
type VersionInfo struct {
  GitTag    string `json:"git_tag"`
  GitCommit  string `json:"git_commit"`
  GitTreeState string `json:"git_tree_state"`
  BuildDate  string `json:"build_date"`
  GoVersion  string `json:"go_version"`
  Compiler   string `json:"compiler"`
  Platform   string `json:"platform"`
}

func (info VersionInfo) String() string {
  return info.GitTag
}

func Get() VersionInfo {
  return VersionInfo{
    GitTag:    gitTag,
    GitCommit:  gitCommit,
    GitTreeState: gitTreeState,
    BuildDate:  buildDate,
    GoVersion:  runtime.Version(),
    Compiler:   runtime.Compiler,
    Platform:   fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
  }
}

主要定义了一个结构体, 保持版本信息.

有些信息可以通过 runtime 获取, 有些是编译时传进来的.

这里没有明确的版本号, 而是使用 git tag 作为版本标签.

最后, 定义一个命令 version.

package cmd

import (
  "encoding/json"
  "fmt"

  "github.com/spf13/cobra"
  "tzh.com/web/pkg/version"
)

var versionCmd = &cobra.Command{
  Use:  "version",
  Short: "Print the version info of server",
  Long: "Print the version info of server",
  Run: func(cmd *cobra.Command, args []string) {
    printVersion()
  },
}

func printVersion() {
  info := version.Get()
  infoj, err := json.MarshalIndent(&info, "", " ") // 加一点缩进
  if err != nil {
    fmt.Printf("遇到了错误: %v\n", err)
  }
  fmt.Println(string(infoj))
}

别忘了使用 AddCommand 添加子命令.

// 初始化, 设置 flag 等
func init() {
  cobra.OnInitialize(initConfig)
  rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "./conf/config.yaml", "config file (default: ./conf/config.yaml)")
  rootCmd.AddCommand(versionCmd)
}

由此, 代码基本已经改完了, 还剩下最后一点, 修改 Makefile 文件,

以便简化操作过程.

修改 Makefile

SHELL := /bin/bash
BASEDIR = $(shell pwd)

# build with version infos
versionDir = "tzh.com/web/pkg/version"
gitTag = $(shell if [ "`git describe --tags --abbrev=0 2>/dev/null`" != "" ];then git describe --tags --abbrev=0; else git log --pretty=format:'%h' -n 1; fi)
buildDate = $(shell TZ=UTC date +%FT%T%z)
gitCommit = $(shell git log --pretty=format:'%H' -n 1)
gitTreeState = $(shell if git status|grep -q 'clean';then echo clean; else echo dirty; fi)

ldflags="-w -X ${versionDir}.gitTag=${gitTag} -X ${versionDir}.buildDate=${buildDate} -X ${versionDir}.gitCommit=${gitCommit} -X ${versionDir}.gitTreeState=${gitTreeState}"

all: gotool build
build:
  go build -ldflags ${ldflags} ./
run:
  go run -ldflags ${ldflags} ./
docker:
  go run -ldflags ${ldflags} ./ -c ./conf/config_docker.yaml

首行定义了运行的 shell, 默认是 /bin/sh, 这里改成了更常用的 /bin/bash.

然后, 就是定义了一大堆需要的参数.
在运行 go build 的时候添加了参数 -ldflags ${ldflags}.

如此, 以后只要使用 make build 就能生成具有版本信息的二进制文件了.

编译好之后, 可以运行 ./web version 查看具体的版本信息.

总结

通过为编译时添加额外信息, 可以生成更具交互性的二进制文件.
同时, 也能体会到 Makefile 带来的便捷.

当前部分的代码
作为版本 v0.12.0

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 详解Go语言RESTful JSON API创建

    详解Go语言RESTful JSON API创建

    这篇文章主要介绍了详解Go语言RESTful JSON API创建,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • Go语言使用HTTP包创建WEB服务器的方法

    Go语言使用HTTP包创建WEB服务器的方法

    这篇文章主要介绍了Go语言使用HTTP包创建WEB服务器的方法,结合实例形式分析了Go语言基于HTTP包创建WEB服务器客户端与服务器端的实现方法与相关注意事项,需要的朋友可以参考下
    2016-07-07
  • 使用Go语言解析动态JSON格式的方法

    使用Go语言解析动态JSON格式的方法

    本篇文章主要介绍了使用Go语言解析动态JSON格式的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • Golang高性能持久化解决方案BoltDB数据库介绍

    Golang高性能持久化解决方案BoltDB数据库介绍

    这篇文章主要为大家介绍了Golang高性能持久化解决方案BoltDB数据库介绍,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2021-11-11
  • 深入学习Golang并发编程必备利器之sync.Cond类型

    深入学习Golang并发编程必备利器之sync.Cond类型

    Go 语言的 sync 包提供了一系列同步原语,其中 sync.Cond 就是其中之一。本文将深入探讨 sync.Cond 的实现原理和使用方法,帮助大家更好地理解和应用 sync.Cond,需要的可以参考一下
    2023-05-05
  • Go高级特性探究之协程池详解

    Go高级特性探究之协程池详解

    在并发编程中,协程是 Go 语言的核心特性之一,本文将介绍如何使用 Go 协程池构造一个协程池,并解决函数传参问题、优雅关闭协程池和保证协程安全的问题,感兴趣的可以了解一下
    2023-06-06
  • go语言中的二维切片赋值

    go语言中的二维切片赋值

    这篇文章主要介绍了go语言中的二维切片赋值操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • Golang 探索对Goroutine的控制方法(详解)

    Golang 探索对Goroutine的控制方法(详解)

    下面小编就为大家分享一篇Golang 探索对Goroutine的控制方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12
  • Golang优雅保持main函数不退出的办法

    Golang优雅保持main函数不退出的办法

    很多时候我们需要让main函数不退出,让它在后台一直执行,下面这篇文章主要给大家介绍了关于Golang优雅保持main函数不退出的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-07-07
  • Go读取文件与写入文件的三种方法操作指南

    Go读取文件与写入文件的三种方法操作指南

    在 Go 语言中也经常会遇到操作文件的需求,下面这篇文章主要给大家介绍了关于Go读取文件与写入文件的三种方法操作,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2022-09-09

最新评论