When to use the blank identifier
Fast recall
Quick revision
Core answer
_ 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 as var _ io.Reader = (*File)(nil). 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.Give a selection rule for the blank identifier, show a practical example, and explain when a named variable is clearer.
Saying “it depends” without naming the condition that changes the choice.
State the default choice and the specific reason to move away from it.
Interview-ready guide
Interview answer
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 it because the behaviour is part of the requirement, not simply because the syntax is available.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. This small case shows the reason for the choice directly.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. The two choices solve related problems but communicate different guarantees.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). Otherwise, use a named variable when it keeps the code simpler.
When to use the blank identifier
Direct Answer
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 as var _ io.Reader = (*File)(nil). 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.
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.
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 as var _ io.Reader = (*File)(nil). 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.
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 _, name := range names {
fmt.Println(name)
}
value, _ := strconv.Atoi(input) // unsafe if the error matters
Common Mistakes & Pitfalls
Saying “it depends” without naming the condition that changes the choice.
Check your understanding
Answer these without looking back at the article.
- What are the blank identifier in Go, and how do they work?
- How do the blank identifier compare with a named variable 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.