Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Get number of currently running/active goroutines

Posted on February 23, 2023February 23, 2023 by admin

NumGoroutine function of runtime package can be used to know the currently running/active goroutines.

https://golang.org/pkg/runtime/#NumGoroutine

Below is the signature of the function

func NumGoroutine() int

Working Code:

package main
import (
"fmt"
"runtime"
"time"
)
func main() {
for i := 0; i < 20; i++ {
go execute()
}
fmt.Println(runtime.NumGoroutine())
}
func execute() {
time.Sleep(time.Second * 10)
}

Output:

21
  • golang
  • goroutines
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP