Finding CPU Core Count in Go

Cihan Ozhan
1 min readNov 8, 2021

Finding CPU Core Count in Go

I wrote this code in 2016 but re-shared it for newbies.

With the development of technology, the need for applications to work faster and more efficiently has emerged. In fact, this need is felt much more in distributed architectures and cloud technologies. For this reason, the CPUs and GPUs of the computers we use are developing even faster and even this is insufficient. Because the expectations for software are increasing much faster.

The Go programming language also has a nice built-in ‘runtime’ feature for this requirement. By obtaining basic information such as the number of processors and cores of the machine in your Go application, you can ensure that your application works accordingly and has performance within the framework of the resources you specify.

package main

import (
"fmt"
"runtime"
)

func main() {
cores := runtime.NumCPU()
fmt.Printf("This machine has %d CPU cores. \n", cores)
runtime.GOMAXPROCS(cores)
}

Output : This machine has 4 CPU cores.

Good luck!
Cihan Özhan

--

--