Junior — Middle
Does the Go language know about the number of available CPU cores?
sobes.tech AI
Answer from AI
Yes, the Go language provides the ability to find out the number of available CPU cores through the runtime package. The runtime.NumCPU() function returns the number of logical CPU cores available to the system.
Example:
package main
import (
"fmt"
"runtime"
)
func main() {
fmt.Println("Number of available cores:", runtime.NumCPU())
}
Additionally, you can control the number of cores used for goroutine execution with runtime.GOMAXPROCS(n).