My Go function is expected to return a value, but it may panic when calling a library function. I can use recover()
to capture this in a deferred call, but how
Using the example in icza's answer, and if you wonder what happens if you recover from the panic but don't assign the value for the named return, like this:
func main() {
fmt.Println("Returned:", MyFunc()) // false
}
func MyFunc() (ret bool) {
defer func() {
if r := recover(); r != nil {
}
}()
panic("test")
return true
}
The function will return the zero value of the specified return type.
Run the example in the Go Playground