Can Two Errors.new With The Same Text
Fast recall
Quick revision
Core answer
Practical understanding of Errors New.
Repeating a definition without saying when to use it.
Give one trade-off and say how you would test it.
Interview-ready guide
Interview answer
No — and this is by design. errors.New allocates a new error value each call; two of them with the same string have the same message but different identities. errors.New("not found") == errors.New("not found") is false. The == comparison on errors compares pointers, and the pointers differ.
If you need error equality, the pattern is the sentinel: create the error once at package level and let everyone compare against that one value.
var ErrNotFound = errors.New("not found")
// both sides reference THE SAME value
if err == ErrNotFound { ... } // works — identity match
if errors.Is(err, ErrNotFound) { ... } // also works, and survives wrappingThe errors.Is form is the modern one because it also matches when some layer wrapped the sentinel with %w — the identity is preserved through the chain. Raw == is fast but blind to wrapping, so Is is the default and == the optimization you make when you control both sides of the comparison.
The failure mode to avoid: matching by message text. if err.Error() == "not found" breaks the moment anyone edits a message — identity is stable, prose isn't.
Can Two Errors.new With The Same Text
Direct Answer
No. Each call creates a distinct value — equality is identity, not text. Compare with errors.Is against a shared sentinel.
Deep Dive & Architecture
Why does identity work this way? Because the error interface is satisfied by a pointer type (*errorString), and interface values containing pointers compare by the pointer:
Two interface values are == when their dynamic types match AND their dynamic values are ==. For pointer types, value equality is pointer equality — same allocation. Two errors.New calls mean two allocations, two pointers, inequality. Same string inside is irrelevant.
This is precisely the property that makes sentinel errors reliable. If equality were text-based, then any error anywhere with the message 'not found' would match your sentinel — including ones that mean something subtly different. Identity restricts matching to the one value you deliberately created, and the package-exported var makes that value addressable for comparison.
The pattern generalizes: io.EOF, sql.ErrNoRows, fs.ErrNotExist — the standard library's sentinels are all single values, and the ecosystem matches them with errors.Is for exactly this reason. The identity isn't a quirk to work around; it's the feature.
Common Mistakes & Pitfalls
Repeating a definition without saying when to use it.
Check your understanding
Answer these without looking back at the article.
- What is Errors New, and when would you use it?
- When would you use Errors New in a real project?
- How would you compare Errors New with an alternative?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.