Catch Errors

Examples in Go
You can return a value after recovering a function by using named result parameters.
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"
}
package main

import (
	"fmt"
)

func main() {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("Recovered in main: ", r)
		}
	}()

	badFunction()
	fmt.Println("We didn't exit")

	panic("failed in main")
}

func badFunction() {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("Recovered in function: ", r)
		}
	}()

	panic("failed in function")
}