Building on the other answers, with recent go versions it's also possible to write a buildid to an ELF section - though that's not so easily readable from within the program.
I write the same value to both, using something like the following:
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...)
I use this with mage
, a build tool written in go. You don't need the extra flags above, but I chose those to strip as much information as possible from release binaries.
(off topic: Mage requires a bit more upfront work than something like Make, but is much easier to extend/maintain than a make-based build system - plus you don't have to switch mental gears between go and some other syntax.)