This is a creation in Article, where the information may have evolved or changed.
Original: Golang automatically generate version information
Demand
The Golang program automatically generates version information when build, using./helloworld–version to view version and build time
Implementation principle
Use the link option -X
to set a variable that can be accessed in a binary file
12345678 |
Package mainimport"FMT"var"No Version provided"func Main () { fmt. Println ("HelloWorld version is:", Version)} |
123 |
"-X Main. Version 1.5 "1.5 |
12345678910 |
Package mainimport \ "Fmt\" var buildstamp = \ "No timestamp set\" var githash = \ "No Githash set\" Func main () { fmt. Println (\ "HelloWorld buildstamp is:\", Buildstamp) FMT. Println (\ "HelloWorld buildgithash is:\", Githash)} |
123456 |
Go build-ldflags \"x main.buildstamp ' date \ ' +%y-%m-%d_%i:%m:%s\ ' x main.githash ' git rev-parse HEAD ' \" HelloWorld . Go ./helloworld HelloWorld buildstamp Is:2015-09-08_05:58:49helloworld Buildgithash is: 1adb00d88d832687eb4148a3871829fb73021c29 |
Resources
Golang-auto-build-versioning
Some other related introductions:
- Write git version information to go program at compile time
- Command/link
- Setting Go variables from the outside