在Go语言中,空字符串可以作为map的键。

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

Empty string as map key in Go?

问题

在Go语言中,将空字符串""作为map的键存储是好还是坏的做法?这似乎是一个特殊情况,不太适合作为键进行存储。你认为呢?

英文:

Is it a good or bad practice to store to store "" as a map key in Go? It seems like a special case which wouldnt be ideal for storage as a key. What do you think?

答案1

得分: 8

空的string""string类型的有效值,因此它是具有string作为键类型的映射的有效键值。在这方面没有什么“边缘”情况。

是否有意义存储与空字符串键相关联的值取决于您的用例。这没有好坏之分。如果对您有意义,请使用它。

英文:

The empty string value "" is a valid value for the string type, so it's a valid key value for maps having string as the key type. There's nothing "edge" case in that.

Whether it makes sense to store a value associated with the empty string key really depends on your use case. There's nothing good or bad in it. Use it if it has meaning for you.

huangapple
  • 本文由 发表于 2021年6月15日 04:19:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/67976701.html
匿名

发表评论

匿名网友

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

确定