Go语言 之goroutine特性
package main import ( "fmt" "time" ) func Test() { var i int for i < 10 { fmt.Printf("Test %d", i) i++ time.Sleep(time.Second * 1) } } func main() { go Test() fmt.Println("main exit") }
主goroutine退出后,其它的工作goroutine也会自动退出
package main import ( "fmt" "time" ) func Test() { var i int for i < 10 { fmt.Printf("Test %d", i) i++ time.Sleep(time.Second * 1) } } func main() { go Test() fmt.Println("main exit") }
主goroutine退出后,其它的工作goroutine也会自动退出