You can use named result parameters . Name your return values and in the deferred function, when a panic was detected, you can change the values of the returned "variables". Changed new values will be returned.
Example:
func main() {
fmt.Println("Returned:", MyFunc())
}
func MyFunc() (ret string) {
defer func() {
if r := recover(); r != nil {
ret = fmt.Sprintf("was panic, recovered value: %v", r)
}
}()
panic("test")
return "Normal Return Value"
}
Exit (try on the Go Playground ):
Returned: was panic, recovered value: test
: :
, , , , .
, :
.
: :
doParse
, nil
-deferred .