Blank Identifier (Underscore)
We know that we can't declare a variable unless we use it in the Go language. Every variable declared in the Go language must be used someplace in our code.
Unused variables are variables that the user has defined throughout the programme but has never used. The programme is nearly unreadable because of these variables. The programmer is not allowed to define an unused variable; if you do, the compiler will throw an error.
To solve this problem, Go provides a blank identifier. We can use the blank identifier (_) when we have a value generally allocated to a variable, but it will not be used anywhere in our code.
Example
Below is the implementation of a blank identifier in Go. We have imported the “bufio” package for reading input from the console along with “fmt” and “os.” Let’s have a look at the implementation of the program.
//using blank identifier in go
package main
//importing bufio package for reading input from console along with fmt and os
import (
"fmt"
"bufio"
"os"
)
//main function
func main() {
fmt.Println("Enter your name:")
reader := bufio.NewReader(os.Stdin)
//reading input from console
text, _ := reader.ReadString('\n')
fmt.Println("Hello, " + text)
}
To use the blank identifier, type underscore (_) in place of the variable name, which isn't used anywhere else in the programme. In the above example, we have typed underscore while reading the input from the console.
Output

Uses of the Blank Identifier
Now, we will see the various scenarios where we use a blank identifier. Different use cases have been explained in the following sections.
Unused Imports
If you import a package in Go, you must use it; otherwise, a compiler error will occur. We can fix this compilation problem by using a blank identifier.
In the below example, let’s see what happens when we import a package but don't use it in the program.
//using blank identifier in unused imports
package main
import "fmt"
func main() {
a := 1
b := 2
a = a + b
}
Here, we imported the package “fmt” but did not use it elsewhere in the program. So let’s look at the error that the compiler throws.

To solve this problem, we can use the blank identifier as demonstrated in the below example.
//using blank identifier in unused imports
package main
import _ "fmt"
func main() {
a := 1
b := 2
a = a + b
}
If you try to execute this code, it will compile without any error, as shown below. Note: The output is blank because we are not printing anything to the console window.

Ignore Values
We may also sometimes need to ignore some values in Go programming. The blank identifier allows you to ignore values you don’t require in the program.
//using blank identifier for ignoring values
package main
import "fmt"
//defining a function with return value
func functionName() (int, int) {
return 1, 2
}
//main function
func main() {
a, _ := functionName()
fmt.Println(a)
}
In the above example, we have ignored the value of the second parameter to be passed to the function “functionName.” So, the function will only return the value of the first parameter, as shown in the following output image.

Ignore Compilation Errors
For some reason, the blank identifier can be used as a placeholder where the variables would be ignored. Later, those variables can be added back in by replacing the operator. It helps in the debugging of code in many circumstances.
In the below example, we have used the blank identifier knowingly, to avoid the compile-time error.
//using blank identifier for ignoring compilation errors
package main
import "fmt"
func main() {
//blank identifier is used to avoid compilation errors
var _ = fmt.Println
}
The use of a blank identifier here could be for debugging purposes. So, the output of the above code is blank but is successfully compiled instead of throwing any error.

Frequently Asked Questions
Is Go a static or dynamic language?
Go or Golang is a statically typed language with a syntax based on C but adds features like garbage collection (like Java), type safety, and some dynamic typing.
Should I learn Go or learn rust?
Operating systems, file systems, and gaming engines are all possible with Rust. Go is ideally suited for big data, machine learning, and enormous file editing applications. Golang is a compiler-based language that may be easily compiled for any target operating system, including Linux and Mac, on a development workstation.
Why is Golang so popular?
Go's created applications have been tested and proven to be extraordinarily performant and scalable. Golang is a highly efficient language, similar to C and C++, that handles parallelisms similarly to Java and has code readability similar to Python and Pearl. Golang has undeniable architectural advantages over its predecessors.
Why is Golang so popular among startups?
Golang is popular among startups because it allows for speedy implementation and is appropriate for microservice development (an architectural style that develops an app as a set of small services). This is a significant benefit for organisations manufacturing speciality products with limited capabilities.
Conclusion
This article extensively discussed the basics of identifiers in Golang and blank identifier and their uses. We implemented the program to demonstrate various uses of the blank identifier in the Go language.
We hope this blog has helped you enhance your knowledge regarding “Blank Identifier in Go.” If you would like to learn more, check out our articles on “Returning Pointer from a Function in Go,” “Defer in Go,” “Go Loops,” “Pointer to an Array as Function Argument in Go,” and “Go Routines.” Do upvote our blog to help other ninjas grow.
Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations and much more!
Happy Reading!