How to Get the Current Number of Running Goroutines

Get the current number of running goroutines in a Go program using the runtime.NumGoroutine() function.

Use runtime.NumGoroutine() to get the current number of running goroutines. This function returns an integer count of all goroutines, including the main one.

import "runtime"

count := runtime.NumGoroutine()

Note that this count is an approximation and may not be perfectly accurate if goroutines are starting or stopping at the exact moment of the call.