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")
```
No answers yet.
Log in to leave an answer
Blogs
Caching in PHP: Redis, Memcached, and Built-in Solutions for Site Acceleration
Bitwise Operations in C++: How to Set, Clear, and Toggle a Bit
Byte Array from Stream in C#: 5 Best Practices 2025
Catching Bugs on the Fly: Monitoring and Logging JavaScript Applications