Skip to content

Instantly share code, notes, and snippets.

@parsibox
Created June 14, 2023 12:00
Show Gist options
  • Save parsibox/ee4f1642fb95d1f77c2885694df94376 to your computer and use it in GitHub Desktop.
Save parsibox/ee4f1642fb95d1f77c2885694df94376 to your computer and use it in GitHub Desktop.
view all running Goroutines in golang
fmt.Println(runtime.NumGoroutine())
// Get the stack traces of all currently running Goroutines.
stackTrace := make([]runtime.StackRecord, 1024)
length, ok := runtime.GoroutineProfile(stackTrace)
// Print out the stack traces.
fmt.Println("Stack traces of all Goroutines:")
for i := 0; i < length; i++ {
fmt.Println(stackTrace[i].Stack())
}
// Check if profiling was successful.
if !ok {
fmt.Println("Profiling failed!")
}
stackTrace1 := make([]byte, 1024*1024)
length = runtime.Stack(stackTrace1, true)
// Print out the stack traces.
fmt.Println("Stack traces of all Goroutines:")
fmt.Println(string(stackTrace1[:length]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment