应用程序自动构建版本控制 [英] Application auto build versioning

查看:35
本文介绍了应用程序自动构建版本控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to increment a minor version number automatically each time a Go app is compiled?

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

I would like to set a version number inside my program, with an autoincrementing section:

$ myapp -version
MyApp version 0.5.132

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

Being 0.5 the version number I set, and 132 a value that increments automatically each time the binary is compiled.

在Go中有可能吗?

推荐答案

Go链接器(转到工具链接)可以设置未初始化的字符串变量的值:

The Go linker (go tool link) has an option to set the value of an uninitialised string variable:

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

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

value. Note that before Go 1.5 this option took two separate arguments. Now it takes one argument split on the first = sign.

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

As part of your build process, you could set a version string variable using this. You can pass this through the go tool using -ldflags. For example, given the following source file:

package main

import "fmt"

var xyz string

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

然后:

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

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

In order to set main.minversion to the build date and time when building:

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

如果您不通过这种方式进行初始化而未初始化main.minversion,则它将包含空字符串.

If you compile without initializing main.minversion in this way, it will contain the empty string.

这篇关于应用程序自动构建版本控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆