2

Is there a tool in the official tooling or the third party tool that lets you list all your running goroutines that are running with your main program?

Or something like the observer in Erlang/Elixir?

sumopal
  • 337
  • 2
  • 12
  • List them in what way? They don't have names or IDs. You can get a count of them from [`runtime.NumGoroutine()`](https://golang.org/pkg/runtime/#NumGoroutine). – Adrian Apr 09 '20 at 17:48
  • The closest you could get to "listing all goroutines" would be dumping all their stack traces, as in https://stackoverflow.com/questions/19094099/how-to-dump-goroutine-stacktraces but it doesn't seem like that's what you're looking for. – Adrian Apr 09 '20 at 17:51
  • listing them like `goroutine 1 ` , `goroutine 2 ...`, `goroutine 3 ...` ..... – sumopal Apr 09 '20 at 17:57
  • You'd need to track that yourself; as far as I'm aware Go does not track the timestamp when each goroutine is started, it has no use for that information. – Adrian Apr 09 '20 at 18:00
  • @Adrian: Strictly speaking, Goroutines _do_ have IDs, but [reading them sends you straight to hell](https://godoc.org/github.com/davecheney/junk/id). – Jonathan Hall Apr 09 '20 at 19:05
  • @Flimzy interesting... I can see why he put that note on it, it's definitely WELL outside of what's supported or covered by the Go1 compatibility promise. – Adrian Apr 09 '20 at 19:10

1 Answers1

4

To get a stack trace of all goroutines:

You can use runtime.Stack() with all set to true:

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.

ifnotak
  • 4,147
  • 3
  • 22
  • 36
  • If that is the answer OP is looking for, then the question is a duplicate of https://stackoverflow.com/questions/19094099/how-to-dump-goroutine-stacktraces – Adrian Apr 09 '20 at 18:03
  • Agreed! I would still keep it as it's the closest to what OP is looking for! – ifnotak Apr 09 '20 at 18:18
  • 1
    I think this answer is useful separate from the linked questions. It was just what I was looking for. – enocom Jun 16 '22 at 21:28