--Thanks,jbd
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-dev/CAMGFQi78JcH6yojLnV9CH8OxK2zCROE_Ys9R8FuVTvxP2F4ZCQ%40mail.gmail.com.
On Sat, 12 Jun 2021, 00:04 Jaana Dogan, <rak...@rakyll.org> wrote:Hello all,Currently, it's not discussed in the https://github.com/golang/go/wiki/CodeReviewComments when it's preferred to return a concrete error type.Consider the following custom error type:type StatusError struct {// ...}func (s *StatusError) Error() string { ... }I'd write a function that constructs a new error like below, so the developer can access to the fields and other methods on the StatusError:func NewStatusError(code int) *StatusError { ... }Some Go developers assume that it should be written like the following to highlight that the returned value's type is error.func NewStatusError(code int) error { ... }I don't know why there is such a perception in the community, but it'd be good to clarify that the second style is not superior to the other.The time this pattern might be problematic is if it's possible that NewStatusError might return nil (for example if code represents an HTTP status and is 2xx). In that case, there would be a strong likelihood that you could get a non-nil-but-nil-inside error created by people that just return the result of calling NewStatusError directly.