This article introduces how to print the call stack for debugging Go code.
Print the Stack() function in the runtime package used by the 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 { ((1)*) } } func power1(){ var buf [1024]byte ("power1.....") n := (buf[:], true) (string(buf[:]), n) }
Output result:
power1..... goroutine 5 [running]: main.power1() /home/lanyang/src/:29 +0xec created by /home/lanyang/src/:14 +0x3c
goroutine 1 [sleep]: (0xdf8475800) /home/lanyang/src/:59 +0x107 () /home/lanyang/src/:17 +0x4f 303
The above Go code debugging--an example of printing call stack is all the content I share with you. I hope you can give you a reference and I hope you can support me more.