What is Blank Identifier(underscore) in Golang?

_(underscore) in Golang is known as the Blank Identifier. Identifiers are the user-defined name of the program components used for the identification purpose. Golang has a special feature to define and use the unused variable using Blank Identifier. Unused variables are those variables which are defined by the user throughout the program but he/she never make the use of these variables. These variables make the program almost unreadable. As you know, Golang is more concise and readable programming language so it doesn’t allow the programmer to define an unused variable if you do such, then the compiler will throw an error.
The real use of Blank Identifier comes when a function returns multiple values, but we need only a few values and want to discard some values. Basically, it tells the compiler that this variable is not needed and ignored it without any error. It hides the variable’s values and makes the program readable. So whenever you will assign a value to Bank Identifier it becomes unusable.

Example 1: In the below program, the function mul_div is returning two values and we are storing both the values in mul and div identifier. But in the whole program, we are using only one variable i.e. mul. So compiler will throw an error div declared and not used

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to show the compiler
// throws an error if a variable is
// declared but not used
  
package main
  
import "fmt"
  
// Main function
func main() {
  
    // calling the function
    // function returns two values which are
    // assigned to mul and div identifier
    mul, div := mul_div(105, 7)
  
    // only using the mul variable
    // compiler will give an error
    fmt.Println("105 x 7 = ", mul)
}
  
// function returning two 
// values of integer type
func mul_div(n1 int, n2 int) (int, int) {
  
    // returning the values
    return n1 * n2, n1 / n2
}

chevron_right


Output:



./prog.go:15:7: div declared and not used

Example 2: Let’s make the use of Blank identifier to correct the above program. In place of div identifier just use the _ (underscore). It allows the compiler to ignore declared and not used error for that particular variable.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to the use of Blank identifier
  
package main
  
import "fmt"
  
// Main function
func main() {
  
    // calling the function
    // function returns two values which are
    // assigned to mul and blank identifier
    mul, _ := mul_div(105, 7)
  
    // only using the mul variable
    fmt.Println("105 x 7 = ", mul)
}
  
// function returning two 
// values of integer type
func mul_div(n1 int, n2 int) (int, int) {
  
    // returning the values
    return n1 * n2, n1 / n2
}

chevron_right


Output:

105 x 7 =  735

Important Points:

  • You can use multiple Blank Identifier in the same program. So you can say a Golang program can have multiple variables using the same identifier name which is the blank identifier.
  • There are many cases that arise the requirement of assignment of values just to complete the syntax even knowing that the values will not be going to be used in the program anywhere. Like a function returning multiple values. Mostly blank identifier is used in such cases.
  • You can use any value of any type with the Blank Identifier.


My Personal Notes arrow_drop_up

Believes in Smart Work

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.