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

后端 未结 8 968
耶瑟儿~
耶瑟儿~ 2020-12-04 08:31

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

_, prs := m[\"example\"]

What exactly is \"_,\" doing and w

相关标签:
8条回答
  • 2020-12-04 08:32

    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
    

    (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
    }
    
    0 讨论(0)
  • 2020-12-04 08:33

    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
    }
    
    0 讨论(0)
  • 2020-12-04 08:33

    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.

    Excerpt From:

    The Go Programming Language (Addison-Wesley Professional Computing Series)

    Brian W. Kernighan

    This material may be protected by copyright.

    0 讨论(0)
  • 2020-12-04 08:34

    _ is the blank identifier. Meaning the value it should be assigned is discarded.

    Here it is the value of example key that is discarded. The second line of code would discard the presence boolean and store the value in prs.
    So to only check the presence in the map, you can discard the value. This can be used to use a map as a set.

    0 讨论(0)
  • 2020-12-04 08:48

    It is called the blank identifier and it helps in cases where you wish to discard the value that is going to be returned and not reference it

    Some places where we use it:

    • A function returns a value and you don't intend to use it in the future
    • You want to iterate and need an i value that you will not be using
    0 讨论(0)
  • 2020-12-04 08:51

    An unused variable is not allowed in Golang

    If you were coming from other programming languages this might feel bit difficult to get used to this. But this results in more cleaner code. So by using a _ we are saying we know there is a variable there but we don't want to use it and telling the compiler that does not complain me about it. :)

    0 讨论(0)
提交回复
热议问题