3 ms·
I find that a mustBeNil function is useful: func mustBeNil(err error) { if err != nil { panic(err) // or log.Fatal(err)
by hornetblack 12y ago
I find that a mustBeNil function is useful:
func mustBeNil(err error) {
if err != nil {
panic(err)
// or log.Fatal(err)
}
}
or
func mustBeNil(err error, msg string) {
if err != nil {
log.Fatalf("%s: %s", err, msg)
}
}
;Edit: brackets
- FiloSottile 12y agoPlease, pull that `panic(err)` line. It is widely accepted that is bad practice, and something you should not do. You panic when a program is expected to crash because of irrecoverable corruption. Only.
- sagichmal 12y ago> You panic when a program is expected to crash because of > irrecoverable corruption. Or programmer error.
- dscrd 12y agomustNotBe?
- NateDad 12y agoDon't panic in the middle of your code. You can occasionally use panic during init() when it indicates a programmer error. Otherwise, just return an error.