How to get the number of CPUs available to the Go program using runtime.NumCPU()?

The runtime.NumCPU() function is used to retrieve the number of logical CPUs available to the Go program. You can call it as follows:

package main import ( "fmt" "runtime" ) func main() { numCPU := runtime.NumCPU() fmt.Println("Number of CPUs:", numCPU) }

The runtime.NumCPU() function returns the number of logical CPUs of the machine. It doesn't necessarily represent the number of physical cores. It simply tells how many threads can be executing simultaneously on the system.