Return Value after Recover in Go

Posted by yhuang
Public (Editable by Users)

You can return a value after recovering a function by using named result parameters.

Go
Edit
package main

import "fmt"

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"
}