Last active
July 5, 2020 01:31
-
-
Save codemodify/c4cce6ba45594f51f1c454472f32b424 to your computer and use it in GitHub Desktop.
go-recover-simple.go
This file contains 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/debug" | |
) | |
func main() { | |
// 1. set app crash handler | |
defer appCrashCatcher() | |
// 2. write your concurrent app | |
.... | |
} | |
func appCrashCatcher() { | |
debug.SetPanicOnFault(true) | |
if err := recover(); err != nil { | |
fmt.Fprintf(os.Stderr, "\n\nCRASH: %v", err) | |
fmt.Fprintf(os.Stderr, "\n\nstack: %s", string(debug.Stack())) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment