Don't Panic
Code running in production must avoid panics. Panics are a major source ofcascading failures. If an error occurs, the function must return an error andallow the caller to decide how to handle it.
Bad | Good |
---|---|
|
|
Panic/recover is not an error handling strategy. A program must panic only whensomething irrecoverable happens such as a nil dereference. An exception to this isprogram initialization: bad things at program startup that should abort theprogram may cause panic.
- var _statusTemplate = template.Must(template.New("name").Parse("_statusHTML"))
Even in tests, prefer t.Fatal
or t.FailNow
over panics to ensure that thetest is marked as failed.
Bad | Good |
---|---|
|
|