go 代碼的調試---打印調用堆棧的實例
更新時間:2017年10月30日 09:16:16 作者:奮翼者
下面小編就為大家?guī)硪黄猤o 代碼的調試---打印調用堆棧的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
本文介紹如何打印調用堆棧進行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
以上這篇go 代碼的調試---打印調用堆棧的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
關于Golang中for-loop與goroutine的問題詳解
這篇文章主要給大家介紹了關于Golang中for-loop與goroutine問題的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用golang具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2017-09-09

