go 代碼的調(diào)試---打印調(diào)用堆棧的實例
更新時間:2017年10月30日 09:16:16 作者:奮翼者
下面小編就為大家?guī)硪黄猤o 代碼的調(diào)試---打印調(diào)用堆棧的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
本文介紹如何打印調(diào)用堆棧進行g(shù)o代碼的調(diào)試。
打印堆棧使用的runtime package中的Stack()函數(shù)
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)
}
輸出結(jié)果:
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 代碼的調(diào)試---打印調(diào)用堆棧的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
一文深入探索Go語言中的循環(huán)結(jié)構(gòu)
在編程中,循環(huán)結(jié)構(gòu)扮演著重要的角色,它使我們能夠有效地重復(fù)執(zhí)行特定的代碼塊,以實現(xiàn)各種任務(wù)和邏輯,在Go語言中,for 是 Go 中唯一的循環(huán)結(jié)構(gòu),本文將深入探討Go語言中的for循環(huán)類型以及它們的用法2023-08-08
關(guān)于Golang中for-loop與goroutine的問題詳解
這篇文章主要給大家介紹了關(guān)于Golang中for-loop與goroutine問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用golang具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09

