我是靠谱客的博主 辛勤舞蹈,这篇文章主要介绍go 代码的调试---打印调用堆栈的实例,现在分享给大家,希望可以做个参考。

本文介绍如何打印调用堆栈进行go代码的调试。

打印堆栈使用的runtime package中的Stack()函数

复制代码
1
2
func Stack(buf []byte, all bool) int Stack formats a stack trace of the calling goroutine into buf and returns the number of bytes written to buf. If all is true, Stack formats stack traces of all other goroutines into buf after the trace for the current goroutine.

example

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package main import ( "runtime" "time" "fmt" ) func main() { go power1() for { time.Sleep(time.Duration(1)*time.Minute) } } func power1(){ var buf [1024]byte fmt.Println("power1.....") n := runtime.Stack(buf[:], true) fmt.Println(string(buf[:]), n) }

输出结果:

复制代码
1
2
3
4
5
6
power1..... goroutine 5 [running]: main.power1() /home/lanyang/src/t.go:29 +0xec created by main.main /home/lanyang/src/t.go:14 +0x3c
复制代码
1
2
3
4
5
6
goroutine 1 [sleep]: time.Sleep(0xdf8475800) /home/lanyang/src/t.go:59 +0x107 main.main() /home/lanyang/src/t.go:17 +0x4f 303

以上这篇go 代码的调试---打印调用堆栈的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

最后

以上就是辛勤舞蹈最近收集整理的关于go 代码的调试---打印调用堆栈的实例的全部内容,更多相关go内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(181)

评论列表共有 0 条评论

立即
投稿
返回
顶部