- Blog/
What is _ (underscore) in Go?
Table of Contents
Underscore _ demisified in Go.
Blank identifier #
The blank identifier is represented by the underscore character _. It serves as an anonymous placeholder instead of a regular (non-blank) identifier and has special meaning in declarations, as an operand, and in assignment statements.
Official Docs:
Why we use it? #
As you know, in Golang, every should be used in its declaration scope, But what if you have a variable that is not used, and also irrelevant to you? The compiler will throw an exception, so to Example
Its also common if you use a function that returns more than one value, and you want to ignore one of the values (commonly to test things out you ignore the error) Underscore will be your friend, and if you want more details.
The blank identifier may be used whenever syntax requires a variable name but program logic does not, for instance to discard an unwanted loop index when we require only the element value.
- Copywrite The Go Programming Language (Addison-Wesley Professional Computing Series)
Common use case #
If you see an import statement with underscore, Jon Explains it here Why we import SQL drivers as the blank identifier