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

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

问题描述

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



我想在我的程序中设置一个版本号, autoincrementing部分:

  $ myapp -version 
MyApp版本0.5.132



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



这是可能的吗?

解决方案

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

  -X importpath.name = value 
将importpath named name中的字符串变量的值设置为value。
请注意,在Go 1.5之前,这个选项有两个独立的参数。
现在它在第一个=符号上分割一个参数。

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

 包主
$ b $导入fmt

var xyz string

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

然后:

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

为了设置<$ c $

  go build -ldflagsc> main.minversion 到构建日期和时间: -X main.minversion =`date -u +。%Y%m%d。%H%M%S`service.go 

如果您不以这种方式初始化 main.minversion ,它将包含空字符串。


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

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

Is this possible in 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 value.
    Note that before Go 1.5 this option took two separate arguments.
    Now it takes one argument split on the first = sign.

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)
}

Then:

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

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

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

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

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