Golang builtin.recover() function example

package builtin

The recover built-in function allows a program to manage behavior of a panicking goroutine.

Golang builtin.recover() function usage example

 package main

 import "fmt"

 func startPanic() {
 defer func() {
 if error := recover(); error != nil {
 fmt.Println("Recovering....", error)
 }
 }()
 panic("noooo!")
 }

 func main() {
 fmt.Println("Starting to panic...")
 startPanic()
 fmt.Println("This will APPEAR because of Recover")
 }

Output :

Starting to panic...

Recovering.... noooo!

This will APPEAR because of Recover

Reference :

http://golang.org/pkg/builtin/#recover

Advertisement