Recover stops a panic and returns its value:
func safe() {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered:", r)
}
}()
panic("oops")
}
Recover only works inside a deferred function. It returns nil if there's no panic. After recovering, the function returns normally.