亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2019年10月31日 10:17:30   作者:帥氣貓咪  
這篇文章主要介紹了使用 Go 管理版本的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

簡介

如果你曾經(jīng)運行過 docker version,

就會發(fā)現(xiàn)它提供了很多信息:

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

對于編譯好的二進制文件而言, 獲取版本信息是非常重要的.
盡可能地提供詳細信息, 有利于后期的維護和排錯.

如何實現(xiàn)

對于版本信息等, 有兩種方式,

一種從外部獲取, 比如配置文件等,

另一種從源代碼中獲取, 將配置信息寫死在源代碼中.

這兩種都不太好, 比如編譯時間就不太好確定.
最好是能在 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 的值設(shè)置為編譯時的時間, 即從 $(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"
)

// 構(gòu)建時的版本信息
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),
  }
}

主要定義了一個結(jié)構(gòu)體, 保持版本信息.

有些信息可以通過 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 添加子命令.

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

由此, 代碼基本已經(jīng)改完了, 還剩下最后一點, 修改 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.

然后, 就是定義了一大堆需要的參數(shù).
在運行 go build 的時候添加了參數(shù) -ldflags ${ldflags}.

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

編譯好之后, 可以運行 ./web version 查看具體的版本信息.

總結(jié)

通過為編譯時添加額外信息, 可以生成更具交互性的二進制文件.
同時, 也能體會到 Makefile 帶來的便捷.

當前部分的代碼
作為版本 v0.12.0

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 9個Golang中map常用示例分享

    9個Golang中map常用示例分享

    這篇文章主要和大家分享9個Golang中map可能是常用的使用案例,從1到9,越來越厲害。文中的示例代碼講解詳細,希望對大家學習Golang有一定的幫助
    2023-02-02
  • Go中interface機制的實現(xiàn)

    Go中interface機制的實現(xiàn)

    本文主要介紹了Go中interface機制的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-06-06
  • Go語言中的基礎(chǔ)數(shù)據(jù)類型使用實例

    Go語言中的基礎(chǔ)數(shù)據(jù)類型使用實例

    這篇文章主要為大家介紹了Go中的基礎(chǔ)數(shù)據(jù)類型使用示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • Go?Fiber快速搭建一個HTTP服務器

    Go?Fiber快速搭建一個HTTP服務器

    Fiber?是一個?Express?啟發(fā)?web?框架基于?fasthttp?,最快?Go?的?http?引擎,這篇文章主要介紹了Go?Fiber快速搭建一個HTTP服務器,需要的朋友可以參考下
    2023-06-06
  • Qt6.5 grpc組件使用 + golang grpc server示例詳解

    Qt6.5 grpc組件使用 + golang grpc server

    這篇文章主要介紹了Qt6.5 grpc組件使用+golang grpc server示例,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-05-05
  • go中sync.RWMutex的源碼解讀

    go中sync.RWMutex的源碼解讀

    本文主要介紹了go中sync.RWMutex的源碼解讀,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-04-04
  • golang切片原理詳細解析

    golang切片原理詳細解析

    這篇文章主要介紹了golang切片原理詳細解析,切片在編譯時定義為Slice結(jié)構(gòu)體,并通過NewSlice()函數(shù)進行創(chuàng)建,更多相關(guān)內(nèi)容感興趣的小伙伴可以參考一下下面文章內(nèi)容
    2022-06-06
  • 手把手教你導入Go語言第三方庫

    手把手教你導入Go語言第三方庫

    本文主要介紹了手把手教你導入Go語言第三方庫,通過導入gin包來深入學習,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • go語言題解LeetCode1299將每個元素替換為右側(cè)最大元素

    go語言題解LeetCode1299將每個元素替換為右側(cè)最大元素

    這篇文章主要為大家介紹了go語言LeetCode刷題1299將每個元素替換為右側(cè)最大元素示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • Go語言中實現(xiàn)完美錯誤處理實踐分享

    Go語言中實現(xiàn)完美錯誤處理實踐分享

    Go?語言是一門非常流行的編程語言,由于其高效的并發(fā)編程和出色的網(wǎng)絡(luò)編程能力,越來越受到廣大開發(fā)者的青睞。本文我們就來深入探討一下Go?語言中的錯誤處理機制吧
    2023-04-04

最新評論