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

  1. if _, err := os.Stat(path); os.IsNotExist(err) {
  2. fmt.Printf("%s does not exist\n", path)
  3. }

Unused imports and variables

  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "log"
  6. "os"
  7. )
  8. var _ = fmt.Printf // For debugging; delete when done.
  9. var _ io.Reader // For debugging; delete when done.
  10. func main() {
  11. fd, err := os.Open("test.go")
  12. if err != nil {
  13. log.Fatal(err)
  14. }
  15. // TODO: use fd.
  16. _ = fd
  17. }

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

  1. if _, ok := val.(json.Marshaler); ok {
  2. fmt.Printf("value %v of type %T implements json.Marshaler\n", val, val)
  3. }

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)