Avatar ·

How to get stacktrace of all goroutines in Go for debugging

Hi! I'm writing a multithreaded application in Go that uses many goroutines. At some point, the program starts behaving unstable — sometimes it hangs, sometimes it throws strange errors. I suspect the problem is a deadlock or a goroutine leak (when they are created but never finish). I need to understand which goroutines are currently running and on which lines of code they are stopped. I know that in Java you can use `jstack` or `Thread.getAllStackTraces()`, but in Go I haven't found an equivalent. For example, I have this code: ```go package main import ( "fmt" "sync" "time" ) func main() { var wg sync.WaitGroup wg.Add(2) go func() { defer wg.Done() time.Sleep(10 * time.Second) fmt.Println("goroutine 1 done") ```