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

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

    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.

提交回复
热议问题