Blank identifier and predeclared identifiers
The blank identifier discards values; predeclared identifiers are built into the language and still obey ordinary name-binding rules.
Canonical guidance
_is a write-only placeholder that discards a value- names like
len,make,error,nil,true, andfalseare predeclared by the language - predeclared identifiers can still be shadowed, but doing so is usually confusing
Use when
- reading multi-value assignments
- reviewing imports kept only for side effects
- explaining built-in names
Avoid
- discarding errors casually
- naming locals
len,error, ornil - assuming
_creates storage or a usable variable
Preferred pattern
v, ok := m[key]
if !ok {
return
}
_ = v
Anti-pattern
- using
_to ignore an error that should affect control flow
Explanation: This is tempting when plumbing code quickly, but ignored errors erase the branch that tells the program what actually happened.
Why
_and the predeclared names appear everywhere in Go code, so misunderstanding them has broad review impact