Skip to content

Instantly share code, notes, and snippets.

@nwillc
Last active July 14, 2022 14:58
Show Gist options
  • Save nwillc/019fc24269f4d5897a97e6e7dbbea7e2 to your computer and use it in GitHub Desktop.
Save nwillc/019fc24269f4d5897a97e6e7dbbea7e2 to your computer and use it in GitHub Desktop.
Go showing basic panic recover
func CaculateSomething(i int) (result int, err error) {
// Does a bunch of math to calculate a value, or
// returns an error when it can't.
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %+v", r)
}
}()
// ... the magic ...
return result, err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment