An interface value in Go is a pair of a dynamic type and a dynamic value, and it equals nil only when both are unset. If you store a nil *MyError in an error variable, the type part is *MyError, so err != nil is true even though the pointer inside is nil. This go nil interface gotcha is a classic cause of "why does my function report an error when there is none".
The fix is to return a literal nil for the interface, never a typed nil pointer.
Internally a non-empty interface holds two words: a pointer to type information (and the method table) and a pointer to the data. An empty interface any holds a type pointer and a data pointer too. The interface is nil only when the type word is nil.
| Interface value | Type word | Data word | == nil |
|---|---|---|---|
var e error | nil | nil | true |
var p *MyErr; var e error = p | *MyErr | nil | false |
e = &MyErr{} | *MyErr | address | false |
The second row is the gotcha: the pointer is nil but the interface is not, because it still records a type.
The function succeeded, yet the caller sees a non-nil error. Calling err.Error() would also panic, since the receiver is nil and the method reads e.Msg.
Return an untyped nil on success, and keep the concrete pointer inside the failing branch only.
Another safe rule: declare a variable of the interface type, not the concrete type, when it may stay empty.
Never declare var err *MyError and return it as error. The same trap applies to custom error types used by errors.As: declare the target pointer, but return plain nil from your own functions.
If you receive an interface that might hold a nil pointer, you can test with reflection, but this is a last resort.
Using this in production hides the real defect. Fix the producer instead, so callers can trust err != nil.
Because the dynamic type is known, a method call on the typed nil dispatches normally to (*MyError).Error, with a nil receiver. It only panics when the body dereferences the receiver. You can make it safe, but it is better not to reach this state.
This avoids a panic but still leaves the non-nil interface, so callers still take the error path.
Look for these patterns:
newErr() *MyError whose result is returned directly as error.go vet does not catch every case. Linters such as staticcheck and nilness analysers flag some of them, and the project's Go FAQ describes this exact behaviour under "Why is my nil error value not equal to nil?". A unit test that calls the function on its success path and asserts err == nil catches the bug right away.
The error interface now carries the pointer's type, so it is no longer the zero interface. Only an interface with no type and no value equals nil.
Yes, every interface type behaves this way, including any, io.Reader and your own interfaces.
Only when you intend a non-nil interface with a nil receiver and your methods handle it. That is rare and surprising, so avoid it.
Return nil explicitly on success and compare with err != nil as usual. Do not wrap results of typed pointers.
An interface is nil only when both its dynamic type and value are nil. Assigning a nil *T to an interface gives a non-nil interface, which makes err != nil true on success. Always return a literal nil for interface results, declare error variables with the interface type, and add a test that checks the success path.