How golang error reporting function gets compilation information

How can a function set the following data from the runtime so that it can handle the appropriate error messages as part of an error library to be used across many products (we're switching to golang):

  • Compilation date and time of the executable
  • The compilation machine used to create the executable

I would like to be able to get both of them to increment the different version numbers of the files that I can report along with the stack trace information

Useful but off-topic information:
 - You can get stack traces from the runtime as shown here http://technosophos.com/2014/03/19/generating-stack-traces-in-go.html
 - Reflected package http://golang.org/pkg/reflect/ can be used to check the identified function

Thanks for the help,
Richard

+3


source to share


1 answer


You can use the -X linker flag to set the value of the string variable on creation:

go build -ldflags "-X main.Uname '$(uname -a)' -X main.CompileTime '$(date)'"

      

With a command like this, this code

package main

import "fmt"

// Set by the linker.
var CompileTime, Uname string

func main() {
    fmt.Println(Uname)
    fmt.Println(CompileTime)
}

      



will print something like

Linux user 3.13.0-53 Wed May 20 10:34:39 UTC 2015 x86_64 GNU/Linux
Wed May 27 12:00:00 UTC 2015

      

See the linker docs for more information .

+5


source







All Articles