Skip to content

Instantly share code, notes, and snippets.

@dictav
Last active August 29, 2015 14:06
Show Gist options
  • Save dictav/8905be4058df12180b79 to your computer and use it in GitHub Desktop.
Save dictav/8905be4058df12180b79 to your computer and use it in GitHub Desktop.
package main
import (
"runtime"
"fmt"
"math/rand"
"time"
)
func main() {
defer func() {
if err := recover(); err != nil {
trace := make([]byte, 1024)
count := runtime.Stack(trace, true)
fmt.Printf("Recover from panic: %s\n", err)
fmt.Printf("Stack of %d bytes: %s\n", count, trace)
}
}()
outer()
}
func outer() {
rand.Seed(int64(time.Now().UnixNano()))
switch rand.Intn(4)+1 {
case 1: inner1()
case 2: inner2()
case 3: inner3()
case 4: inner4()
}
}
func inner1() {
panic("Fake error1!")
}
func inner2() {
panic("Fake error2!")
}
func inner3() {
panic("Fake error3!")
}
func inner4() {
panic("Fake error4!")
}
@dictav
Copy link
Author

dictav commented Sep 12, 2014

http://play.golang.org/p/l811QPNk-L

Go Playground では UnixNano() 使えない?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment