Skip to content
Preptima
hardConceptCodingMidSeniorStaff

Your function returns nil on the success path, the caller checks err != nil, and the check fires anyway. What happened?

A Go interface is non-nil when it holds a concrete type, even if the concrete pointer value is nil. Returning a typed nil pointer as an error creates an interface value that fails err == nil checks.

5 min readUpdated 2026-08-15

Answer

Short answer

In Go, an interface value contains both a dynamic type and a value. If a function returns a typed nil pointer as error, the interface has a type even though the pointer is nil, so err != nil is true. Return a plain nil error instead of a typed nil pointer.

Keep interfaces explicit in the answer because that is the concept the interviewer is actually trying to test. A good interfaces explanation names the trade-off, the failure mode, and the evidence you would use before choosing. Use interfaces once more at the decision point so the answer reads as judgement rather than a detached example.

Keep interfaces explicit in the answer because that is the concept the interviewer is actually trying to test. A good interfaces explanation names the trade-off, the failure mode, and the evidence you would use before choosing.

Was this answer helpful?

Likely follow-ups

  • Rewrite the function so the bug is impossible rather than merely absent.
  • Why does a nil map read work while a nil map write panics?
  • How would errors.As behave against this value?
  • Where else does Go compare a two-word value in a way that surprises people?

Related questions

Further reading