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

后端 未结 8 1016
耶瑟儿~
耶瑟儿~ 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: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.

提交回复
热议问题