How to print the stack trace of the current execution thread of the program in Go

1 Answer

0 votes
package main

import (
	"fmt"
	"runtime/debug"
)

func main() {
	fmt.Println("Start")
	myFunc()
	fmt.Println("The end.")
}

func myFunc() {
	debug.PrintStack()
}




/*
run:

Start
goroutine 1 [running]:
runtime/debug.Stack()
	/usr/local/go/src/runtime/debug/stack.go:26 +0x5e
runtime/debug.PrintStack()
	/usr/local/go/src/runtime/debug/stack.go:18 +0x13
main.myFunc(...)
	/tmp/0AKiYjjLx3/main.go:15
main.main()
	/tmp/0AKiYjjLx3/main.go:10 +0x50
The end.

*/



answered Jul 19, 2025 by avibootz

Related questions

1 answer 91 views
2 answers 100 views
1 answer 114 views
3 answers 195 views
1 answer 141 views
141 views asked Apr 30, 2025 by avibootz
...