Go: Blank identifier (underscore)

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 (

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

func main() {
        f, err := os.Open("test.go")
        if err != nil {
        _ = 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.


Be the first to comment!