Golang : Get number of 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 :
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
Tutorials
+6.7k Golang : How to solve "too many .rsrc sections" error?
+24.2k Golang : Time slice or date sort and reverse sort example
+5.9k Golang : Extract XML attribute data with attr field tag example
+7.2k Golang : How to convert strange string to JSON with json.MarshalIndent
+8k Golang : Check if integer is power of four example
+11.1k Golang : How to flush a channel before the end of program?
+11.4k Golang : Simple file scaning and remove virus example
+30.6k error: trying to remove "yum", which is protected
+14.1k Golang : How to convert a number to words
+18.7k Golang : Check whether a network interface is up on your machine
+20.5k Golang : Saving private and public key to files
+14.9k Golang : Save(pipe) HTTP response into a file