是否可以在每次编译Go应用程序时自动增加次要版本号?
我想在我的程序中设置一个版本号,使用一个自动递增的部分:
$ myapp -version
MyApp version 0.5.132
0.5是我设置的版本号,132是每次编译二进制文件时自动递增的值。
这在围棋中可能吗?
是否可以在每次编译Go应用程序时自动增加次要版本号?
我想在我的程序中设置一个版本号,使用一个自动递增的部分:
$ myapp -version
MyApp version 0.5.132
0.5是我设置的版本号,132是每次编译二进制文件时自动递增的值。
这在围棋中可能吗?
当前回答
在其他答案的基础上,在最近的go版本中,也可以向ELF段编写buildid语句——尽管在程序中不太容易读取。
我写了相同的值给两者,使用如下的东西:
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编写的构建工具。您不需要上面的额外标志,但我选择这些标志是从发布二进制文件中尽可能多地剥离信息。
(题外话:与Make相比,Mage需要更多的前期工作,但比基于Make的构建系统更容易扩展/维护-另外,你不必在go和其他语法之间切换思维。)
其他回答
在其他答案的基础上,在最近的go版本中,也可以向ELF段编写buildid语句——尽管在程序中不太容易读取。
我写了相同的值给两者,使用如下的东西:
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编写的构建工具。您不需要上面的额外标志,但我选择这些标志是从发布二进制文件中尽可能多地剥离信息。
(题外话:与Make相比,Mage需要更多的前期工作,但比基于Make的构建系统更容易扩展/维护-另外,你不必在go和其他语法之间切换思维。)
使用多个-ldflags:
$ go build -ldflags "-X name1=value1 -X name2=value2" -o path/to/output
在构建混合命令行应用程序和库项目时,我在使用-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会将文件内容读入一个变量,增加它,然后将它插入到版本中。转到文件而不是日期,并将新的构建号写回文件。
在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%"
日期格式假设您的环境回声% Date %是Fri 07/22/2016,回声%time%是16:21:52.88
那么输出将是:version=0.0.1, date=2016-07-22T16:21:52
另外,我想发布一个如何使用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文件将创建两个可执行文件。一个是执行函数1,另一个将函数2作为主要入口:
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