上級 › recoverは同じgoroutine内のpanicしか捕まえない › パターン1
recoverは同じgoroutine内のpanicしか捕まえない
recoverはそれを呼び出したgoroutine内で発生したpanicしか回復できません。
1 package main 2 3 import ( 4 "fmt" 5 "time" 6 ) 7 8 func worker() { 9 panic("boom") 10 } 11 12 func main() { 13 defer func() { 14 if r := recover(); r != nil { 15 fmt.Println("recovered:", r) 16 } 17 }() 18 ________________________ ^ 19 time.Sleep(100 * time.Millisecond) 20 }
panic: boom
空欄を埋めて実行を通す
別のgoroutineで発生したpanicは、mainのdeferにあるrecoverでは捕まえられません。recoverはpanicと同じgoroutine内でしか効果がありません。
次の問題