Golang runtime.GOMAXPROCS() and NumCPU() functions example

package runtime

Golang runtime.GOMAXPROCS() and NumCPU() functions usage example

  package main

  import (
 "fmt"
 "runtime"
  )

  func main() {

 cores := runtime.NumCPU()

 fmt.Printf("This machine has %d CPU cores. \n", cores)

 // maximize CPU usage for maximum performance
 runtime.GOMAXPROCS(cores)

  }

References :

http://golang.org/pkg/runtime/#GOMAXPROCS

http://golang.org/pkg/runtime/#NumCPU

https://www.socketloop.com/tutorials/golang-get-number-of-cpu-cores

Advertisement