英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论