Golang : Get number of CPU cores


Tags : golang cpu cores

Problem :

You want to find the maximum number of CPU cores to maximize performance.

Solution :

Use the runtime package to find out the number of CPU cores available on local machine.

 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)

 }

Reference :

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



Tags : golang cpu cores

By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.


Advertisement