package main import ( "fmt" "runtime" "time" ) func test() { defer fmt.Println("end") //return //终止此函数 runtime.Goexit() //终止协程 fmt.Println("ccccc") } //主协程退出,子协程也退出 func main() { //指定运算核数 n := runtime.GOMAXPROCS(1) go func() { for { fmt.Println("sum") time.Sleep(time.Second) } }() go func() { fmt.Println("aaaaa") test() fmt.Println("bbbbb") }() for i := 0; i < 2; i++ { fmt.Println("main") time.Sleep(time.Second) } }