Blank identifier (underscore)

yourbasic.org/golang

The blank identifier _ is an anonymous placeholder. It may be used like any other identifier in a declaration, but it does not introduce a binding.

Ignore values

The blank identifier provides a way to ignore left-hand side values in an assignment.

_, present := timeZone["CET"]

sum := 0
for _, n := range a {
	sum += n
}

Import for side effects

It can also be used to import a package solely for its side effects.

import _ "image/png" // init png decoder function

Silence the compiler

It can be used to during development to avoid compiler errors about unused imports and variables in a half-written program.

package main

import (
    "fmt"
    "log"
    "os"
)

var _ = fmt.Printf // DEBUG: delete when done

func main() {
    f, err := os.Open("test.go")
    if err != nil {
        log.Fatal(err)
    }
    _ = f // TODO: read file
}

For an automatic solution, use the goimports tool, which rewrites a Go source file to have the correct imports. Many Go editors and IDEs run this tool automatically.

Share this page: