English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang basic tutorial

Golang control statements

Golang functions & methods

Golang structs

Golang slices & arrays

Golang strings (String)

Golang pointers

Golang interfaces

Golang concurrency

Golang exceptions (Error)

Other Golang items

Go Blank Identifier (Underscore)

In Golang_(Underscore)is called a blank identifier.IdentifierIs a user-defined name for program components used for identification purposes.

Golang has a special feature that allows the definition and use of unused variables using blank identifiers. An unused variable refers to a variable that is defined throughout the program but never used by the user.variables.
. These variables make the program almost unreadable. As you know, Golang is a more concise and readable programming language, so it does not allow programmers to define unused variables. If you do so, the compiler will throw an error. When a function returns multiple values, the blank identifier is truly used, but we only need a few values and want to discard some values. Basically, it tells the compiler that this variable is not needed and ignores it without any error. It hides the variable's value and makes the program readable. Therefore, whenever you assign a value to Bank

Example1:Identifier, it becomes unavailable.in the following program, the functionmul_divWhen returning two values, weBoth values are stored inmulanddivIdentifier in. But in the entire program, we only use one variable, namelymul. So the compiler will throw an error div declared and not used

package main
import "fmt"
func main() {
    //Call the function
    //The function returns two values
    //Assigned to mul and div identifier
    mul, div := mul_div(105, 7)
    //Only use mul variable
    //The compiler will report an error
    fmt.Println("105 x 7 = ", mul)
}
//The function returns two
//Values of integer types
func mul_div(n1 int, n2 int) (int, int) {
    //Return value
    return n1 * n2, n1 / n2
}

Output:

# command-line-arguments
.\test.go:10:7: div declared but not used

Example2:Let's use a blank identifier to correct the above program. Instead of the div identifier, just use _ (underscore). It allows the compiler to ignore the error of the specific variable (declared and not used).

package main
import "fmt"
func main() {
    //Call the function
    //The function returns two values
    //Assigned to mul and blank identifier
    mul, _ := mul_div(105, 7)
    //Only use mul variable
    fmt.Println("105 x 7 = ", mul)
}
//The function returns two
//Values of integer types
func mul_div(n1 int, n2 int) (int, int) {
    //Return value
    return n1 * n2, n1 / n2
}

Output:

105 x 7 =  735

Caution:

  • You can use multiple blank identifiers in the same program. Therefore, it can be said that a Golang program can use the same identifier name (i.e., blank identifier) to contain multiple variables.

  • In many cases, even if you know that these values will not be used anywhere in the program, you still need to assign values to complete the syntax. Just like a function that returns multiple values. In this case, a blank identifier is usually used.

  • You can use any type of value with a blank identifier.