英文:
How can a golang error reporting function get compilation information
问题
一个函数如何从运行时中获取以下细节,以便作为错误库的一部分来构建适当的错误报告,以在多个产品中使用(我们正在转换到golang):
- 可执行文件的编译日期和时间
- 用于创建可执行文件的编译机器
我希望能够检索到这两个信息,以补充各种文件修订号,以及一些堆栈跟踪信息。
有用的相关但与主题无关的信息:
- 您可以从运行时中获取堆栈跟踪,如此示例所示:http://technosophos.com/2014/03/19/generating-stack-traces-in-go.html
- 可以使用reflect包(http://golang.org/pkg/reflect/)来检查已识别的函数。
谢谢您的帮助,
Richard
英文:
How can a function establish the following details from the runtime so that it can craft appropriate error reporting as part of a error library to be used using in many products (we are changing over to golang):
- Compilation date and time of the executable
- Compilation machine used to create the executable
I would like to be able to retrieve both of these to augment the various file revision numbers that I can report along with some stack trace information
Useful related but off-topic information:
- You can get stack traces out of the runtime as illustrated here http://technosophos.com/2014/03/19/generating-stack-traces-in-go.html
- The reflect package http://golang.org/pkg/reflect/ can be used to examine an identified function
Thank you for your assistance,
Richard
答案1
得分: 5
你可以使用-X链接器标志在构建时设置字符串变量的值:
go build -ldflags "-X main.Uname '$(uname -a)' -X main.CompileTime '$(date)'"
使用这样的命令,下面的代码
package main
import "fmt"
// 由链接器设置。
var CompileTime, Uname string
func main() {
fmt.Println(Uname)
fmt.Println(CompileTime)
}
将打印类似于以下内容
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
有关更多信息,请参阅链接器文档。
英文:
You can use the -X linker flag to set the value of a string variable when building:
go build -ldflags "-X main.Uname '$(uname -a)' -X main.CompileTime '$(date)'"
With such command, 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 info.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论