Blank identifier
The blank identifier is represented by the underscore character
_
. It serves as an anonymous placeholder instead of a regular (non-blank) identifier and has special meaning in declarations, as an operand, and in assignment statements.
Ah, the blank identifier!
We’ve talked about it in a few contexts already. Namely, struct field names, as well as binding variables. But it can be used in many places… Including a few surprising places, where it never serves a purpose. This is not at all an exhaustive list, more a reminder of what the blank identifier does.
func() {
_: // A blank label does nothing. Has no scope, cannot be referenced...
var _ = 1 // This blank variable isn't very useful
_, err := doThing() // But this one discards a return value from a function
for _, x := range things { // This blank identifier discards the range index
/* ... */
}
x, _ := someInterface.(int) // Prevent the type assertion from panicking
}
Quotes from The Go Programming Language Specification Version of December 15, 2022