go 代码的调试---打印调用堆栈

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

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

>
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

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)

}

输出结果:

power1…..
goroutine 5 [running]:
main.power1()
/home/lanyang/src/t.go:29 +0xec
created by main.main
/home/lanyang/src/t.go:14 +0x3c

goroutine 1 [sleep]:
time.Sleep(0xdf8475800)
/home/lanyang/src/t.go:59 +0x107
main.main()
/home/lanyang/src/t.go:17 +0x4f
303

博客园文章链接:
http://www.cnblogs.com/lanyangsh/p/7750411.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值