Created
July 14, 2022 14:57
-
-
Save nwillc/1ba67d833b569ccfae897030854f3b13 to your computer and use it in GitHub Desktop.
Crude solution to nil panic
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
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