go语言日期做版本号

go语言开发程序,使用日期作为版本号。

Makefile

❯ cat Makefile
# -------------------------------
# 编译脚本
# -------------------------------

BUILD_NAME         := bcpserv
BUILD_VERSION      := 1.0.1.B.1
BUILD_DATE         := $(shell date "+%y%m%d")
BUILD_TIME         := $(shell date "+%F %T")
BUILD_AUTHOR       := "john"

all:
        go build -ldflags \
                "-X '${BUILD_NAME}/version.Version=${BUILD_VERSION}.${BUILD_DATE}' \
                -X '${BUILD_NAME}/version.Name=${BUILD_NAME}' \
                -X '${BUILD_NAME}/version.BuildTime=${BUILD_TIME}' \
                -X '${BUILD_NAME}/version.Author=${BUILD_AUTHOR}'"

build:
        CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build

clean:
        rm -rf bcpserv

help:
        @echo "make build - 编译 Go 代码, 生成Linux系统的二进制文件"

go代码

❯ cat version/version.go
package version

import (
        "fmt"
        "os"
)

var (
        Name      = "nil"
        Version   = "nil"
        BuildTime = "nil"
        Author    = "nil"
)

func GetVersionInfo() string {
        ver := fmt.Sprintf("原始文件名: %s, 版本: %s, 构建时间: %s, 作者: %s", Name, Version, BuildTime, Author)
        return ver
}

func init() {
        args := os.Args
        if nil == args || len(args) < 2 {
                return
        }
        if "-v" == args[1] {
                fmt.Printf("Name: %s\nVersion: %s\nBuildTime: %s\nAuthor: %s\n", Name, Version, BuildTime, Author)
        } else if "-h" == args[1] {
                fmt.Println("Usage:")
                fmt.Printf("./%s\n", Name)
                fmt.Printf("./%s -v\n", Name)
                fmt.Printf("./%s -h\n", Name)
        }
        os.Exit(0)
}

posted @ 2021-03-30 09:46  jiftle  阅读(203)  评论(0编辑  收藏  举报