Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "_," (underscore comma) in a Go declaration?

Tags:

variables

go

And I can't seem to understand this kind of variable declaration:

_, prs := m["example"] 

What exactly is "_," doing and why have they declared a variable like this instead of

prs := m["example"] 

(I found it as part of Go by Example: Maps)

like image 254
Kansuler Avatar asked Jan 04 '15 10:01

Kansuler


People also ask

What does underscore mean in go?

_(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.

How do you declare variables in type GO?

Go also provides another concise way to declare variables. This is known as short hand declaration and it uses := operator. name := initialvalue is the short hand syntax to declare a variable.

What is declaration in Golang?

Short Variable Declaration Operator(:=) in Golang is used to create the variables having a proper name and initial value. The main purpose of using this operator to declare and initialize the local variables inside the functions and to narrowing the scope of the variables.

What means Golang?

Go (also called Golang or Go language) is an open source programming language used for general purpose. Go was developed by Google engineers to create dependable and efficient software. Most similarly modeled after C, Go is statically typed and explicit.


2 Answers

It avoids having to declare all the variables for the returns values.
It is called the blank identifier.

As in:

_, y, _ := coord(p)  // coord() returns three values; only interested in y coordinate 

That way, you don't have to declare a variable you won't use: Go would not allow it. Instead, use '_' to ignore said variable.

(the other '_' use case is for import)

Since it discards the return value, it is helpful when you want to check only one of the returned values, as in "How to test key existence in a map?" shown in "Effective Go, map":

_, present := timeZone[tz] 

To test for presence in the map without worrying about the actual value, you can use the blank identifier, a simple underscore (_).
The blank identifier can be assigned or declared with any value of any type, with the value discarded harmlessly.
For testing presence in a map, use the blank identifier in place of the usual variable for the value.

As Jsor adds in the comments:

"generally accepted standard" is to call the membership test variables "ok" (same for checking if a channel read was valid or not)

That allows you to combine it with test:

if _, err := os.Stat(path); os.IsNotExist(err) {     fmt.Printf("%s does not exist\n", path) } 

You would find it also in loop:

If you only need the second item in the range (the value), use the blank identifier, an underscore, to discard the first:

sum := 0 for _, value := range array {     sum += value } 
like image 77
VonC Avatar answered Nov 15 '22 16:11

VonC


The Go compiler won't allow you to create variables that you never use.

for i, value := range x {    total += value } 

The above code will return an error message "i declared and not used".

Since we don't use i inside of our loop we need to change it to this:

for _, value := range x {    total += value } 
like image 27
Juni Brosas Avatar answered Nov 15 '22 18:11

Juni Brosas