Skip to content

Instantly share code, notes, and snippets.

@nwillc
Created July 14, 2022 14:57
Show Gist options
  • Save nwillc/1ba67d833b569ccfae897030854f3b13 to your computer and use it in GitHub Desktop.
Save nwillc/1ba67d833b569ccfae897030854f3b13 to your computer and use it in GitHub Desktop.
Crude solution to nil panic
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.
done := false
defer func() {
if !done {
err = fmt.Errorf("panic: %+v", recover())
}
}()
// ... the magic ...
done = true
return result, err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment