Junior — Middle
Is it possible to control the distribution of processor cores for executing goroutines?
sobes.tech AI
Answer from AI
In Go, you can control the number of operating system threads used to execute goroutines with the function runtime.GOMAXPROCS(n). This function sets the maximum number of threads that can simultaneously execute Go code.
However, you cannot directly bind specific goroutines to particular CPU cores — this is managed by the Go scheduler and the operating system.
Example of setting the number of threads:
package main
import (
"fmt"
"runtime"
)
func main() {
// Set to use 4 cores
runtime.GOMAXPROCS(4)
fmt.Println("Maximum number of threads:", runtime.GOMAXPROCS(0))
}
Thus, you can limit or increase parallelism, but precise distribution across cores cannot be controlled.