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]