panic 能夠改變程序的控制流,調用 panic 后會立刻停止執行當前函數的剩余代碼,并在當前 goroutine 中遞歸執行調用方的 defer。
// A _panic holds information about an active panic.
//
// A _panic value must only ever live on the stack.
//
// The argp and link fields are stack pointers, but don't need special
// handling during stack growth: because they are pointer-typed and
// _panic values only live on the stack, regular stack pointer
// adjustment takes care of them.
type _panic struct {argp unsafe.Pointer // pointer to arguments of deferred call run during panic; cannot move - known to liblinkarg any // argument to paniclink *_panic // link to earlier panicpc uintptr // where to return to in runtime if this panic is bypassedsp unsafe.Pointer // where to return to in runtime if this panic is bypassedrecovered bool // whether this panic is overaborted bool // the panic was abortedgoexit bool
}
panic:
- 創建新的 panic函數并添加到所在 goroutine 的 _panic 鏈表的最前面。
- 在循環中不斷從當前 goroutine 的 _defer 中鏈表獲取 defer函數并調用執行。
- 調用 runtime.fatalpanic 中止整個程序。
runtime.fatalpanic 實現了無法被恢復的程序崩潰: - 它在中止程序之前會通過 runtime.printpanics 打印出全部的 panic 消息以及調用時傳入的參數。
- 打印崩潰消息后會調用 runtime.exit 退出當前程序并返回錯誤碼 2,程序的正常退出也是通過 runtime.exit 實現的。
panic() 會退出進程,是因為調用了 exit 的系統調用。