英文:
exec.Command does not register error from Go's own pprof tool
问题
这是我的代码:
cmd := exec.Command("go", "tool", "pprof", "-dot", "-lines", "http://google.com")
out, err := cmd.Output()
if err != nil {
panic(err)
}
println(string(out))
当我在控制台中运行完全相同的命令时,我看到:
$ go tool pprof -dot -lines http://google.com
正在从 http://google.com/profilez 获取配置文件
请稍候...(30秒)
服务器响应:404 Not Found
然而,我的Go程序没有注册这是一个错误。奇怪的是,变量out打印为空字符串,err为nil。发生了什么?
为了澄清,我正在对http://google.com进行性能分析,以故意创建一个错误。通常我会对一个真实的Go应用程序进行性能分析。
英文:
Here is my code:
cmd := exec.Command("go", "tool", "pprof", "-dot", "-lines", "http://google.com")
out, err := cmd.Output()
if err != nil {
panic(err)
}
println(string(out))
When I run the exact same command in my console, I see:
$ go tool pprof -dot -lines http://google.com
Fetching profile from http://google.com/profilez
Please wait... (30s)
server response: 404 Not Found
However, my go program does not register that this is an error. Oddly, the variable out prints as an empty string and err is nil. What is going on?
To clarify, I am profiling http://google.com to purposefully create an error. I would normally profile a real Go application.
答案1
得分: 2
获取个人资料从http://google.com/profilez
请稍等...(30秒)
服务器响应:404未找到
写入到stderr。您的程序捕获stdout,但stdout为空。考虑调用:
out,err:= cmd.CombinedOutput()
来获取stdout和stderr。
cmd.Output()和cmd.CombinedOutput()返回err == nil,因为命令以零状态退出。也许应该提出一个问题,要求命令以非零状态退出。
英文:
The text
Fetching profile from http://google.com/profilez
Please wait... (30s)
server response: 404 Not Found
is written to stderr. Your program captures stdout, which is empty. Consider calling:
out, err := cmd.CombinedOutput()
to grab both stdout and stderr.
cmd.Output()
and cmd.CombinedOutput()
return err == nil
because the command exits with status zero. Perhaps an issue should be filed requesting that the command exit with non-zero status.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论