A goroutine is a function that is capable of running concurrently with other functions. To create a goroutine we use the keyword go
followed by a function invocation:
package main import "fmt" func f(n int) { for i := 0; i < 10; i++ { fmt.Println(n, ":", i) } } func main() { go f(0) var input string fmt.Scanln(&input) }
结果:
0 : 0 0 : 1
多次运行的结果
0 : 0 0 : 1 0 : 2 0 : 3 0 : 4 0 : 5 0 : 6
有疑问加站长微信联系(非本文作者)