Skip to content

Instantly share code, notes, and snippets.

@spiegel-im-spiegel
Last active February 14, 2016 09:41
Show Gist options
  • Save spiegel-im-spiegel/89526909cc206f31c1d7 to your computer and use it in GitHub Desktop.
Save spiegel-im-spiegel/89526909cc206f31c1d7 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io"
"os"
"runtime"
)
func main() {
os.Exit(run(os.Stderr))
}
func run(log io.Writer) (exit int) {
defer func() {
if r := recover(); r != nil {
fmt.Fprintf(log, "Panic: %v\n", r)
for depth := 0; ; depth++ {
pc, _, line, ok := runtime.Caller(depth)
if !ok {
break
}
fmt.Fprintf(log, " -> %d: %s: (%d)\n", depth, runtime.FuncForPC(pc).Name(), line)
}
exit = 1
}
}()
f()
exit = 0
return
}
func f() {
numbers := []int{0, 1, 2}
fmt.Println(numbers[3]) //panic!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment