Gaaker Go

Golang Blank identifiers

The underscore (_) is called a blank identifier and is used to declare or assign anonymous variables In Go.

Any type can be assigned to a blank identifier, and any value assigned to this identifier will be discarded.

For the twoVals function, I only want to receive the second return value, and I can use the blank identifier to ignore the first return value.

_, b = twoVals()

The following is a complete code example.

package main

import "fmt"

func main() {
    var b int
    _, b = twoVals()
    fmt.Println("Second return value:", b)
}

func twoVals(a int, b int) (int, int) {
	return 4, 7
}

Below is the program output:

Second return value: 7
< Blank identifiers Blank identifiers >