What is the blank identifier in Go?

The _ (underscore) is known as the blank identifier in Go. We can use the blank identifier to declare and use the unused variables. The unused variables are the variables that are only defined once in the program and not used. These variables affect the readability of the program and make it unreadable. The unique feature of Go is that it is a readable and concise language. It never allows a variable to be defined and never used; when the variable is defined and not used in a program, it throws an error.

Uses of a blank identifier

It can ignore values returned by a function if the value is not needed to make the program readable. We can use it many times in a single program. It can ignore the effects of unused imports and compiler errors.

Example 1

package main
import "fmt"
func dummy() (int,int) {
val1 := 10
val2 := 12
return val1,val2
}
func main(){
rVal, _ := dummy()
fmt.Println(rVal)
}

Explanation

  • Line 1: We create the package main.
  • Line 3: We import the library fmt for print statements.
  • Line 4: We create a function dummy() returns two values.
  • Line 10: The main function contains the driver code.
  • Line 11: The rVal receives the first value returned by the function dummy(), and _ tells the program to ignore the second value.

Example 2

package main
import _"fmt"
func main(){
operand1 := 1
operand2 := 2
_ = operand1 + operand2
}

Explanation

  • Line 3: The _ bypasses the error caused by not using the imported library.

  • Line 8: The _ here tells the compiler to ignore the error for now, and we can add the variable later.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved