Although goroutines are fast and you can execute thousands of goroutines on your machine, this comes at a price. In this section, we are going to talk about the go statement, its behavior, and what happens when you start new goroutines in your Go programs.
Notice that closured variables in goroutines are evaluated when the goroutine actually runs and when the go statement is executed in order to create a new goroutine. This means that closured variables are going to be replaced by their values when the Go scheduler decides to execute the relevant code. This is illustrated in the following Go code, which is saved as cloGo.go:
package main import ( "fmt" "time" ) func main() { for i := 0; i <= 20; i++ { go func() { fmt.Print(i, " ") }() } time.Sleep(time.Second...