[Golang] How to get compile time automatically in the Golang program

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. After the program is published, it is often necessary to identify the version number of the program to determine whether some problems are caused by the program not being updated, which is the most efficient way to output compile time in the program startup log. In Golang there is no C language __file__,__line__,__date__,__time__ these useful compile-time constants for us to use, fortunately Golang provides a powerful cgo in the go code embedded in the C code method, you can achieve this demand. Nonsense not much to say, directly on the code:

Package Main

/#
Constchar*build_time (void)
{
    Staticconstchar* psz_build_time=  "["__date__"" __     time__"]";
  return  psz_build_time;    
}
#/
Import"C"

Import(
   "FMT"     
)

var(
Buildtime =  c.gostring (c.build_time (  ))  
)

Funcbuildtime ()string{
  return  buildtime    
}

FuncMain (){
  fmt  .    Printf ("Buildtime is:%s\ n") ,buildtime ())
}

Output:
Build time is: [Jan  2  23:02:20]

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.