|
|
|
|
|
by tapirl
1265 days ago
|
|
> panics are easy to trigger (e.g. methods with nil receivers) No, methods nil receivers in Go don't trigger panics (if they are not dereferenced). > every error must be handled explicitly, and every control flow path must be made obvious, no matter how much verbosity this creates. Is this, bad? It is just the recommended way. You are not required to implement your code in this way. |
|
Yes, but in practice most methods do dereference their receivers. Given that, you have two choices:
1. Check for nil receivers explicitly in every method. This is considered unidiomatic and libraries rarely do this.
2. Don't check for nil receivers, and have your method panic on the first dereference with no explicit check. Then, most of your methods can halt partway through in unexpected (and usually undocumented) ways, unless you pay very close attention to this failure mode.
Furthermore, such panics can occur far down the call stack, making it non-obvious from the stack trace where the error is.
Also, this means that, if you upgrade your library so that a method now dereferences its receiver, your library is suddenly no longer backwards-compatible.