The GOMAXPROCS Environment Variable

Let’s learn about the GOMAXPROCS environment variable.


The GOMAXPROCS environment variable allows us to set the number of OS threads (CPUs) that can execute user-level Go code simultaneously. Starting with Go version 1.5, the default value of GOMAXPROCS should be the number of logical cores available in our machine. There is also the runtime.GOMAXPROCS() function, which allows us to set and get the value of GOMAXPROCS programmatically.

If we decide to assign a value to GOMAXPROCS that is smaller than the number of cores in our machine, we might affect the performance of our program. However, using a GOMAXPROCS value that is larger than the number of available cores does not necessarily make our Go programs run faster.

Coding example

As mentioned earlier in this lesson, we can programmatically set and get the value of the GOMAXPROCS environment variable—this is illustrated in maxprocs.go, which will also show additional capabilities of the runtime package. The main() function is implemented as follows:

Get hands-on with 1200+ tech skills courses.