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
+5.5k Golang : Error reading timestamp with GORM or SQL driver
+6.9k Golang : Test a slice of integers for odd and even numbers
+19.8k Golang : How to validate URL the right way
+5k Golang : Reverse text lines or flip line order example
+8.4k Golang : Verify Linux user password again before executing a program example
+10.1k Golang : Simple word wrap or line breaking example
+4.1k Javascript : Get operating system and browser information
+3.8k Golang : Error handling methods
+48.6k Golang : Check if item is in slice/array
+12.7k Golang : invalid character ',' looking for beginning of value
+13.5k Golang : Merge video(OpenCV) and audio(PortAudio) into a mp4 file