218

每次编译 Go 应用程序时是否可以自动增加次要版本号?

我想在我的程序中设置一个版本号,带有一个自动递增部分:

$ myapp -version
MyApp version 0.5.132

0.5 是我设置的版本号,132 是每次编译二进制文件时自动递增的值。

这在 Go 中可能吗?

4

7 回答 7

367

Go 链接器 ( go tool link ) 有一个选项来设置未初始化的字符串变量的值:

-X importpath.name=value
  Set the value of the string variable in importpath named name to

价值。请注意,在 Go 1.5 之前,此选项采用两个单独的参数。现在它需要在第一个 = 符号上拆分一个参数。

作为构建过程的一部分,您可以使用它设置版本字符串变量。您可以go使用-ldflags. 例如,给定以下源文件:

package main

import "fmt"

var xyz string

func main() {
    fmt.Println(xyz)
}

然后:

$ go run -ldflags "-X main.xyz=abc" main.go
abc

为了main.minversion在构建时设置构建日期和时间:

go build -ldflags "-X main.minversion=`date -u +.%Y%m%d.%H%M%S`" service.go

如果您在没有main.minversion以这种方式初始化的情况下进行编译,它将包含空字符串。

于 2012-07-06T03:57:34.093 回答
33

另外,我想发布一个如何使用 git 和 makefile 的小示例:

--- Makefile ----

# This how we want to name the binary output
BINARY=gomake

# These are the values we want to pass for VERSION and BUILD
# git tag 1.0.1
# git commit -am "One more change after the tags"
VERSION=`git describe --tags`
BUILD=`date +%FT%T%z`

# Setup the -ldflags option for go build here, interpolate the variable values
LDFLAGS_f1=-ldflags "-w -s -X main.Version=${VERSION} -X main.Build=${BUILD} -X main.Entry=f1"
LDFLAGS_f2=-ldflags "-w -s -X main.Version=${VERSION} -X main.Build=${BUILD} -X main.Entry=f2"

# Builds the project
build:
    go build ${LDFLAGS_f1} -o ${BINARY}_f1
    go build ${LDFLAGS_f2} -o ${BINARY}_f2

# Installs our project: copies binaries
install:
    go install ${LDFLAGS_f1}

# Cleans our project: deletes binaries
clean:
    if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi

.PHONY: clean install

make 文件将创建两个可执行文件。一种是执行功能一,另一种是以功能二为主入口:

package main

import (
        "fmt"
)

var (

        Version string
        Build   string
        Entry   string

        funcs = map[string]func() {
                "f1":functionOne,"f2":functionTwo,
        }

)

func functionOne() {
    fmt.Println("This is function one")
}

func functionTwo() {
    fmt.Println("This is function two")
}

func main() {

        fmt.Println("Version: ", Version)
        fmt.Println("Build Time: ", Build)

    funcs[Entry]()

}

然后运行:

make

你会得到:

mab@h2470988:~/projects/go/gomake/3/gomake$ ls -al
total 2020
drwxrwxr-x 3 mab mab    4096 Sep  7 22:41 .
drwxrwxr-x 3 mab mab    4096 Aug 16 10:00 ..
drwxrwxr-x 8 mab mab    4096 Aug 17 16:40 .git
-rwxrwxr-x 1 mab mab 1023488 Sep  7 22:41 gomake_f1
-rwxrwxr-x 1 mab mab 1023488 Sep  7 22:41 gomake_f2
-rw-rw-r-- 1 mab mab     399 Aug 16 10:21 main.go
-rw-rw-r-- 1 mab mab     810 Sep  7 22:41 Makefile
mab@h2470988:~/projects/go/gomake/3/gomake$ ./gomake_f1
Version:  1.0.1-1-gfb51187
Build Time:  2016-09-07T22:41:38+0200
This is function one
mab@h2470988:~/projects/go/gomake/3/gomake$ ./gomake_f2
Version:  1.0.1-1-gfb51187
Build Time:  2016-09-07T22:41:39+0200
This is function two
于 2016-09-07T20:48:42.073 回答
32

用于ldflags设置main包中的变量:

带文件main.go

package main

import "fmt"

var (
    version string
    build   string
)

func main() {
    fmt.Println("version=", version)
    fmt.Println("build=", build)
}

然后运行:

go run \
  -ldflags "-X main.version=1.0.0 -X main.build=12082019" \ 
  main.go

建造:

go build -o mybinary \
  -ldflags "-X main.version=1.0.0 -X 'main.build=$(date)'" \ 
  main.go

用于ldflagsnon-main包中设置变量:

带文件config.go

package config

import "fmt"

var (
    Version string
)

func LogVersion() {
    fmt.Println("version=", Version)
}

您还需要文件main.go

package main

import (
    "fmt"
    "github.com/user/repo/config"
}

func main() {
    config.LogVersion()
}

首先构建你的二进制文件:

go build -o mybinary main.go 

找到要设置的变量名的完整路径:

go tool nm <path_to_binary> | grep Version

再次运行并构建二进制文件,但使用ldflags

go run \
  -ldflags "-X github.com/user/repo/config.Version=1.0.0" \
  main.go --version       


go build -o mybinary \
  -ldflags "-X github.com/user/repo/config.Version=1.0.0" \
  main.go     

灵感来自https://github.com/golang/go/wiki/GcToolchainTricks#include-build-information-in-the-executable


另外,如果您正在使用goreleaser,请阅读此https://goreleaser.com/environment/#using-the-mainversion

默认明智的 GoReleaser 设置三个 ldflags:

main.version:当前 Git 标记 main.commit
:当前 git commit SHA
main.date:根据 RFC3339 的日期


如果你想看到这个在行动:https ://github.com/hoto/fuzzy-repo-finder/blob/master/pkg/config/config.go

于 2019-03-10T18:02:00.397 回答
27

在构建混合的命令行应用程序和库项目时,我无法使用该-ldflags参数,因此我最终使用 Makefile 目标来生成包含我的应用程序版本和构建日期的 Go 源文件:

BUILD_DATE := `date +%Y-%m-%d\ %H:%M`
VERSIONFILE := cmd/myapp/version.go

gensrc:
    rm -f $(VERSIONFILE)
    @echo "package main" > $(VERSIONFILE)
    @echo "const (" >> $(VERSIONFILE)
    @echo "  VERSION = \"1.0\"" >> $(VERSIONFILE)
    @echo "  BUILD_DATE = \"$(BUILD_DATE)\"" >> $(VERSIONFILE)
    @echo ")" >> $(VERSIONFILE)

在我的init()方法中,我这样做:

flag.Usage = func() {
    fmt.Fprintf(os.Stderr, "%s version %s\n", os.Args[0], VERSION)
    fmt.Fprintf(os.Stderr, "built %s\n", BUILD_DATE)
    fmt.Fprintln(os.Stderr, "usage:")
    flag.PrintDefaults()
}

但是,如果您想要一个原子增加的构建号而不是构建日期,您可能需要创建一个包含最后一个构建号的本地文件。您的 Makefile 会将文件内容读入一个变量,将其递增,将其插入version.go文件而不是日期,然后将新的内部版本号写回文件。

于 2014-07-28T20:26:32.677 回答
12

在给定以下程序的 Windows 操作系统上

package main

import "fmt"

var (
    version string
    date    string
)

func main() {
    fmt.Printf("version=%s, date=%s", version, date)
}

您可以使用构建

go build -ldflags "-X main.version=0.0.1 -X main.date=%date:~10,4%-%date:~4,2%-%date:~7,2%T%time:~0,2%:%time:~3,2%:%time:~6,2%"

日期格式假设您的环境echo %date%Fri 07/22/2016并且echo %time%16:21:52.88

然后输出将是:version=0.0.1, date=2016-07-22T16:21:52

于 2016-07-22T20:20:39.847 回答
12

使用多-ldflags

$ go build -ldflags "-X name1=value1 -X name2=value2" -o path/to/output
于 2016-06-18T02:29:38.920 回答
0

在其他答案的基础上,使用最近的 go 版本,还可以将 buildid 写入 ELF 部分——尽管从程序中读取它并不容易。

我使用以下内容向两者写入相同的值:

BuildInfo:= "BUILD #x, branch @ rev built yymmdd hh:mm:ss"
// note the nested quotes "''" required to get a string with 
// spaces passed correctly to the underlying tool
ldFl := fmt.Sprintf("-X 'main.buildId=%s' -s -w '-buildid=%s'", BuildInfo, BuildInfo)
args := []string{
    "build",
    "-ldflags", ldFl,
    "-trimpath",
    "-gcflags", "-dwarf=false",
}
buildpath:="path/to/my/cmd"
args=append(args,buildpath)
buildCmd:=exec.Command("go", args...)

我将它与mage用 go 编写的构建工具一起使用。您不需要上面的额外标志,但我选择这些标志是为了从发布二进制文件中删除尽可能多的信息。

(题外话:Mage 比 Make 之类的东西需要更多的前期工作,但比基于 make 的构建系统更容易扩展/维护——另外,你不必在 go 和其他语法之间切换心理齿轮。)

于 2021-01-15T00:13:23.250 回答