Last active
August 29, 2015 14:06
-
-
Save dictav/8905be4058df12180b79 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://play.golang.org/p/l811QPNk-L
Go Playground では UnixNano() 使えない?