Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Recover function in Go

By default an unhandled panic function stops execution of programs, recover function can be used to handle fatal errors. Recover function can be used inside the defer function,

Example : Usage of recover function in Go Lang
package main

import (
    "fmt"
    errors "github.com/pkg/errors"
)

func main() {
    anotherMethod()
    println("After handling")
}

func anotherMethod() {
    defer func() {
        if err := recover(); err != nil {
            fmt.Printf("work failed: %+v \n", err)
        }
    }()
    method()
}

func method() (int, error) {
    panic("fatal error")
    return 1, errors.New("my error")
}



Output
work failed: fatal error
After handling