Essential Go Panic and recover  Suggest an edit

Recovering from panic

A common mistake is to declare a slice and start requesting indexes from it without initializing it, which leads to an “index out of range” panic.

The following code explains how to recover from the panic without exiting the program, which is the normal behavior for a panic.

In most situations, returning an error in this fashion rather than exiting the program on a panic is only useful for development or testing purposes.

type Foo struct {
	Is []int

func (fp *Foo) Panic() (err error) {
	defer PanicRecovery(&err)
	fp.Is[0] = 5
	return nil

func PanicRecovery(err *error) {

	if r := recover(); r != nil {
		if _, ok := r.(runtime.Error); ok {
			*err = r.(error)
		} else {
			*err = r.(error)

func main() {
	fp := &Foo{}
	if err := fp.Panic(); err != nil {
		fmt.Printf("Error: %v\n", err)
Error: runtime error: index out of range

The use of a separate function (rather than closure) allows re-use of the same function in other functions prone to panic.

  ↑ ↓ to navigate     ↵ to select     Esc to close