0
CloudFlare's DNS server, RRDNS, is written in Go and the DNS team used to generate a file called version.go
in our Makefile. version.go
looked something like this:
// THIS FILE IS AUTOGENERATED BY THE MAKEFILE. DO NOT EDIT.
// +build make
package version
var (
Version = "2015.6.2-6-gfd7e2d1-dev"
BuildTime = "2015-06-16-0431 UTC"
)
and was used to embed version information in RRDNS. It was built inside the Makefile using sed
and git describe
from a template file. It worked, but was pretty ugly.
Today we noticed that another Go team at CloudFlare, the Data team, had a much smarter way to bake version numbers into binaries using the -X
linker option.
The -X
Go linker option, which you can set with -ldflags
, sets the value of a string variable in the Go program being linked. You use it like this: -X main.version 1.0.0
.
A simple example: let's say you have this source file saved as hello.go
.
package main
import "fmt"
var who = "World"
func main() {
fmt.Printf("Hello, %s.n", who)
}
Then you can use go run
(or other build commands like go build
or go install
Continue reading