Skip to content

Instantly share code, notes, and snippets.

@hiraq
Created August 28, 2016 14:51
Show Gist options
  • Save hiraq/f103520e03306a232e590e81f969ba18 to your computer and use it in GitHub Desktop.
Save hiraq/f103520e03306a232e590e81f969ba18 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
type Callback func()
func JustPrintIt(msg string) {
fmt.Println(msg)
}
func catchThePanic(next ...Callback) {
if r := recover(); r != nil {
JustPrintIt(fmt.Sprintf("Recovered : %v", r))
if len(next) > 0 {
for _, callback := range next {
callback()
}
}
}
}
func NextCall() {
JustPrintIt("Normal function")
}
func NextPanic() {
defer catchThePanic()
panic("second panic")
}
func DoPanic() {
defer catchThePanic(NextCall, NextPanic, NextCall)
defer JustPrintIt("O")
defer JustPrintIt("F")
defer JustPrintIt("I")
defer JustPrintIt("L")
panic("this is panic")
}
func main() {
DoPanic()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment