go 提前退出
go - 退出语句
主要分为: return 、Exit、Goexit
用例代码:
package main
import (
"fmt"
"runtime"
"time"
)
/*
return 退出当前函数
os.Exit(-1) 退出当前进程(程序)
runtime.Goexit() 退出当前go程,只退出最近一层
*/
func main() {
go func() {
go func() {
fmt.Print("this is go 11111\n")
// return
// os.Exit(-1)
runtime.Goexit()
fmt.Print("this is go 22222\n")
}()
time.Sleep(1*time.Second)
fmt.Print("this is go 3333\n")
}()
time.Sleep(2 * time.Second)
fmt.Print("this is 主进程\n")
}