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

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

提交回复
热议问题