Golang automatically generate version information

Source: Internet
Author: User
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

    • Example 1:
12345678
 Package mainimport"FMT"var"No Version provided"func Main () {    fmt. Println ("HelloWorld version is:", Version)}
123
"-X Main. Version 1.5 "1.5
    • Example 2
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:

    1. Write git version information to go program at compile time
    2. Command/link
    3. Setting Go variables from the outside
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.