Jay Taylor's notes

back to listing index

go - Golang application auto build versioning - Stack Overflow

[web search]
Original source (stackoverflow.com)
Tags: golang go ldflags compiler stackoverflow.com
Clipped on: 2013-07-11

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?

asked Jul 6 '12 at 0:52
Image (Asset 1/4) alt= 5,30632234
add comment

3 Answers

up vote 26 down vote accepted

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

-X symbol value
Set the value of an otherwise uninitialized string variable.
The symbol name should be of the form importpath.name,
as displayed in the symbol table printed by "go tool nm".

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
answered Jul 6 '12 at 3:57
Image (Asset 2/4) alt= 1,862712
  upvote
 flag
Will that value be saved into the binary if I use go bouild instead of go run? – Sebastián Grignoli Jul 6 '12 at 4:17
  upvote
 flag
I just tried it and it works. Thanks! – Sebastián Grignoli Jul 6 '12 at 4:34
1 upvote
 flag
go build -ldflags "-X main.minversion `date -u +.%Y%m%d%.H%M%S`" service.go – Sebastián Grignoli Jul 6 '12 at 4:49
  upvote
 flag
This does not seem to work for me in Go 1.1.1: $ go run -ldflags "-X main.xyz abc" main.go -> go run: no go files listed – felixge 11 hours ago
add comment

I don't think it's possible in go.

Maybe you can have a version.go which contains a Build variable, and your build script will increment it before running go build? (by some clever perl/sed/awk one liner).

answered Jul 6 '12 at 1:36
Image (Asset 3/4) alt= 2,945718
add comment

Not with Go's own tool chain. You'll have to hook a script into your build setup somewhere that could do it.

You can write one in Go itself. This is how I would go about it:

  • Once you start a build cycle, hook this tool in there somewhere.
  • Use the source parsing packages to create an AST of your main.go.
  • Traverse it and find a specific variable or constant you have reserved for the version information.
  • Extract the version value, parse it into integers and increment where necessary.
  • Write the changes back to the AST.
  • Write the entire AST back into the file as source code (this is what gofmt does).
  • Exit this little tool and continue with the build process.
answered Jul 6 '12 at 1:40
Image (Asset 4/4) alt= 3,90811121
  upvote
 flag
Building a syntax tree and rewriting my script with it would be a little too much for me. I think I will define a constant and increment it's value changing just that line of the source file with a simple text replace. It will work as long as the constant is there, just like with a AST, but simpler. Thanks! – Sebastián Grignoli Jul 6 '12 at 2:36
1 upvote
 flag
Very much over the top. I was unaware of the approach axw listed. Which is, of course, infinitely better. Live and learn! – jimt Jul 9 '12 at 21:48
add comment

Your Answer

 
community wiki

Not the answer you're looking for? Browse other questions tagged or ask your own question.