Goexit
package main
import (
"fmt"
"runtime"
)
func test() {
defer fmt.Println("ccccccccc") //只要成功注册了无论是return 还是goexit都会被调用
runtime.Goexit() //退出函数所在当前go程
defer fmt.Println("ddddddddddd") //这里没注册成功,因为在注册之前已经退出go程了
}
func main() {
go func() {
fmt.Println("aaaaaaa")
test()
defer fmt.Println("bbbbbbb")
}()
for {
}
}
//aaaaaaa
//ccccccccc