Go's blank identifier versus a named variable
Fast recall
Quick revision
Core answer
_ is better only when retaining that value would add no meaning and the discard is deliberate. Use _ to discard information that is truly irrelevant; never use it merely to make an important error disappear.Compare the blank identifier and a named variable using the same example, then give a clear selection rule.
Calling one option universally better without explaining the requirement.
Compare semantics and failure behaviour before discussing convenience or performance.
Interview-ready guide
Interview answer
A named variable preserves information and should be used when the value affects correctness, logging, or debugging.
_is better only when retaining that value would add no meaning and the discard is deliberate. They are related choices, but they do not promise the same behaviour.For example: the range index is intentionally ignored, so
_is clear and safe. Ignoring the parsing error is different because invalid input then becomes indistinguishable from a valid zero. The alternative makes a different guarantee, so the code should reveal which guarantee the caller needs.Use
_for an unneeded range index or value, for an intentionally ignored return value only when failure is impossible or irrelevant, and for compile-time interface assertions such asvar _ io.Reader = (*File)(nil). Choose a named variable when its simpler or more explicit behaviour better matches the requirement.Use
_to discard information that is truly irrelevant; never use it merely to make an important error disappear. Document or test the boundary when a future change could silently alter that decision.
Go's blank identifier versus a named variable
Direct Answer
A named variable preserves information and should be used when the value affects correctness, logging, or debugging. _ is better only when retaining that value would add no meaning and the discard is deliberate. Use _ to discard information that is truly irrelevant; never use it merely to make an important error disappear.
Deep Dive & Architecture
When a valid-looking zero appears unexpectedly, search for discarded errors or values near parsing, I/O, type assertions, and database calls.
Restore the named value and handle it at the source.
A named variable preserves information and should be used when the value affects correctness, logging, or debugging. _ is better only when retaining that value would add no meaning and the discard is deliberate. Use _ to discard information that is truly irrelevant; never use it merely to make an important error disappear.
The dangerous mistake is using _ to silence an error. The code may continue with a zero or partial result and fail later, far from the real cause. Name and handle errors, keep _ for truly irrelevant values, and add a comment when a side-effect import or deliberate discard is not obvious.
Code Example
for _, value := range values { use(value) }
n, err := strconv.Atoi(input)
if err != nil { return err }
use(n)
Common Mistakes & Pitfalls
Calling one option universally better without explaining the requirement.
Check your understanding
Answer these without looking back at the article.
- What are the blank identifier in Go, and how do they work?
- When should you use the blank identifier in Go?
- What common mistakes should you avoid with the blank identifier in Go?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.