July 2019
Intermediate to advanced
458 pages
12h 12m
English
Organizing code in multiple goroutines can be helpful to split the work between processors and has many other advantages, as we will see in the next chapters. Since they are so lightweight, we can create a number of goroutines very easily using loops:
func main() { for i := 0; i < 10; i++ { go fmt.Println(i) } time.Sleep(time.Nanosecond)}
The full example is available at https://play.golang.org/p/Jaljd1padeX.
This example prints a list of numbers from 0 to 9 in parallel, using concurrent goroutines instead of doing the same thing sequentially in a single goroutine.
Read now
Unlock full access