How do I get the amount of entries in a map in Go?

huangapple go评论79阅读模式
英文:

How do I get the amount of entries in a map in Go?

问题

你好!你可以使用len函数来获取Go语言中映射(map)中的条目数量,而不需要使用循环来计数。以下是示例代码:

count := len(mymap)

这样就可以直接得到映射中的条目数量了。希望对你有帮助!如果你还有其他问题,请随时提问。

英文:

I'm trying to get the amount of entries in a map in Go. Is there a better way than:

count := 0
for key := range mymap {
    count++
}

Thanks!

答案1

得分: 4

len函数对此非常有效:

http://golang.org/ref/spec#Length_and_capacity

英文:

The len function works well for this:

http://golang.org/ref/spec#Length_and_capacity

huangapple
  • 本文由 发表于 2013年11月11日 12:04:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/19898632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定