Why a Go Error Is Not Nil: Fix Typed Nil Returns and Check As Targets
Reproduce typed nil errors in Go, fix success paths with explicit nil returns, and distinguish As matches from non-nil target pointers, including wrapping.
If a Go function returning error succeeds but its caller still enters err != nil, check whether it returned a nil pointer of a concrete type inside an interface. An explicit return nil on the success path avoids this problem. A small executable example compares the cause and fix, then shows why finding a type with errors.As() may still require checking the resulting pointer.
An empty translucent capsule inside a metal frame beside a completely empty slot. A conceptual illustration of a typed nil value versus a nil interface.
Written and documentation checked: 2026-12-01. Tested environment: Linux amd64, Go 1.24.7. Standard library only. The code does not depend on a specific recent Go feature; the online documentation's version should not be confused with the version actually executed.
A nil pointer can become a non-nil error
The value of var detail *JobError is nil. Returning it as error leaves the dynamic type *JobError in the interface, distinguishing it from a nil interface. The official Go FAQ on nil errors explains this distinction and explicit nil returns.
If a server operation returns a nil pointer to mean success but its caller interprets it as failure, the design can trigger unnecessary failure responses or retries. This article does not run a server or retry system; those effects are application scenarios inferred from the return contract.
| Return form | Comparison of error with nil | Caller interpretation |
return nil in an error return position | Equal | Success |
| Return a nil *JobError as error | Unequal | Appears to contain an error |
| Return &JobError{...} | Unequal | Actual failure |
This table compares the example's error type. It is not a recommendation to build a universal reflection-based nil helper.
Fix the producer and inspect wrapping
Save the following as typed_nil_demo.go. The operation name in JobError is illustrative text; no speech synthesis is performed.
badJob() returns one pointer variable on both success and failure. fixedJob() instead creates a concrete error only on failure and explicitly returns nil on success. This repairs the producer's success contract instead of adding exceptional nil detection to every caller.
The example's Error() handles a nil receiver. Handling that receiver does not change the interface's nil comparison. A real method that accesses fields through a nil pointer may introduce a separate panic risk, but this example does not execute that panic.
errors.As() finds and sets a matching value in an error tree; see the errors.As documentation. Here, the typed nil itself has the matching type, so As returns true while the target pointer is nil. A successful type match and permission to access fields are different conditions. Custom As and Unwrap implementations are outside this example's scope.
Actual execution results
The following output was produced with Go 1.24.7. Every require() check passed.
The incorrect success return was a non-nil error, and errors.Is(bad, nil) was false too. The corrected success return was nil. Actual failures from both implementations, and a wrapped actual failure, retained the expected type and field. Wrapping the typed nil with %w did not eliminate the nil target pointer produced by As.
Wrapping therefore does not repair a success return. Establish the correct return contract first, then add context only when an error exists. These checks verify error values, types, and fields; they do not verify log formatting or HTTP status mapping.
Applying the lesson to service code
New functions should return nil for success and a real error for failure. Tests should check err == nil, rather than relying on printed strings. If an existing API returns a concrete error pointer, its nil case can be handled before converting it to error. Check that API's contract before changing every caller, however.
When inspecting an external error that already contains a typed nil, a condition such as errors.As(err, &detail) && detail != nil can state the requirement for field access. That is a defensive caller condition, not permission for the producer to keep returning an incorrect success value. This article does not propose a universal wrapper that hides internal nil values across all errors, which could discard meaningful failures.
Execution was limited to the custom error above and standard %w wrapping. Custom error traversal in other libraries, concurrency, performance, and production server response paths were not verified. Inspect the return type and actual nil comparison on the success path before trusting nil-looking output.