GO 中 runtime.goexit() 和 os.exit() 的区别
runtime.goexit() 只是退出当前的goroutinue os.exit()会退出主进程
package main import ( "fmt" "os" "runtime" "sync" "time" ) var wg sync.WaitGroup func testFunc1(wg *sync.WaitGroup) { defer wg.Done() for i := 0; i < 10; i++ { fmt.Println("testFunc1") time.Sleep(1*1000000000) } runtime.Goexit() } func testFunc2(wg *sync.WaitGroup){ defer wg.Done() for{ fmt.Println("testFunc2") time.Sleep(1*1000000000) } } func testFunc3(wg *sync.WaitGroup){ defer wg.Done() for i:=0; i<1000000000000;i++{ continue } os.Exit(0) } func testFun4() { os.Exit(12) } func main() { wg.Add(1) //go testFunc1(&wg) go testFunc2(&wg) //go testFunc3(&wg) go testFun4() wg.Wait() }
邮箱: 1090055252@qq.com