The blank identifier can be assigned or declared with any value of any type, with the value discarded harmlessly.
It’s a bit like writing to the Unix /dev/null
file.
The blank identifier in multiple assignment
if _, err := os.Stat(path); os.IsNotExist(err) {
fmt.Printf("%s does not exist\n", path)
}
Unused imports and variables
package main
import (
"fmt"
"io"
"log"
"os"
)
var _ = fmt.Printf // For debugging; delete when done.
var _ io.Reader // For debugging; delete when done.
func main() {
fd, err := os.Open("test.go")
if err != nil {
log.Fatal(err)
}
// TODO: use fd.
_ = fd
}
Import for side effect
To import the package only for its side effects, rename the package to the blank identifier:import _ "net/http/pprof"
Interface checks
if _, ok := val.(json.Marshaler); ok {
fmt.Printf("value %v of type %T implements json.Marshaler\n", val, val)
}
The appearance of the blank identifier in this construct indicates that the declaration exists only for the type checking, not to create a variable:var _ json.Marshaler = (*json.RawMessage)(nil)