Doing things concurrently using goroutines
A goroutine is a function that runs concurrently with other goroutines. When a program starts, the Go runtime creates several goroutines. One of these goroutines runs the garbage collector. Another goroutine runs the main
function. As the program executes, it creates more goroutines as necessary. A typical go program may have thousands of goroutines all running concurrently. The Go runtime schedules these goroutines to operating system threads. Each operating system thread is assigned a number of goroutines that it runs using time sharing. At any given moment, there can be as many active goroutines as the number of logical processors:
Number of threads per core * Number of cores per CPU * Number of CPUs
Creating goroutines
Goroutines are an integral part of the Go language. You create goroutines using the go
keyword.
How to do it...
Create goroutines using the go
keyword followed by a function call:
func f() { ...