This subsection will show you how to create many goroutines and the problems that arise from having to handle more goroutines. The name of the program will be moreGoroutines.go and will be presented in three parts.
The first part of moreGoroutines.go is the following:
package main import ( "fmt" "time" )
The second part of the program has the following Go code:
func main() { fmt.Println("Chapter 09 - Goroutines.") for i := 0; i < 10; i++ { go func(x int) { time.Sleep(10) fmt.Printf("%d ", x) }(i) }
This time, the anonymous function takes a parameter named x, which has the value of the i variable. The for loop that uses the i variable creates ten goroutines, one by one.
The last part of the program is the following: ...