2015-01-04 10:07
浏览 301

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)

图片转代码服务由CSDN问答提供 功能建议


  _,prs:= m [“ example”] 

_,”到底在做什么,为什么 他们声明了这样的变量,而不是

  prs:= m [“ example”] 

(I 找到它作为通过示例:地图 的一部分

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

7条回答 默认 最新

  • dongsetan3216 2015-01-04 10:09

    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
    ", 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
    打赏 评论
  • drd94483 2015-01-04 10:14

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

    打赏 评论
  • douzhuo1858 2015-10-03 12:26

    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
    打赏 评论
  • dongqiang1894 2018-06-28 00:32

    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
    打赏 评论
  • dongma7796 2018-07-14 05:00

    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.

    打赏 评论
  • doucheng7234 2018-12-27 09:33

    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. :)

    打赏 评论
  • dtddjq9637 2019-05-03 01:37

    Basically, _, known as the blank identifier. In GO we can't have variables that are not being used.

    As an instance when you iterating through an array if you are using value := range you don't want a i value for iterating. But if you omit the i value it will return an error. But if you declare i and didn't use it, it will also return an error.

    Therefore, that is the place where we have to use _,.

    Also it is used when you don't want a function's return value in the future.

    打赏 评论

相关推荐 更多相似问题