golang为什么我们没有固定的数据结构 [英] golang why don't we have a set datastructure
问题描述
我正在尝试解决#1.4的"go programming lanaguage"练习,该练习需要我有一套.我可以创建一个set类型,但是为什么语言没有附带呢?来自谷歌(番石榴也是起源于谷歌)的,语言设计者为什么不选择增加对基本数据结构的支持?为什么要强迫您的用户为一组基本的东西创建自己的实现?
I'm trying to solve "The go programming lanaguage" exercise #1.4 which requires me to have a set. I can create a set type but why doesn't the language come with one ? go, having come from google, where guava also originated, why didn't the language designers opt for adding support for fundamental data structures ? why force your users to create their own implementations for something so basic as a set ?
推荐答案
部分是因为Go没有泛型(所以您将需要为每个类型使用一个set-type,或者依赖于反射,这是相当低效的)
Partly, because Go doesn't have generics (so you would need one set-type for every type, or fall back on reflection, which is rather inefficient).
部分地,因为如果您需要的只是将单个元素添加/删除到集合中"和相对节省空间",则只需使用 map [yourtype] bool
(并将集合中的任何元素的值设置为 true
),或者,为了提高空间效率,您可以使用空结构作为值,并使用 _,present = the_setoid[key]
检查是否存在.
Partly, because if all you need is "add/remove individual elements to a set" and "relatively space-efficient", you can get a fair bit of that simply by using a map[yourtype]bool
(and set the value to true
for any element in the set) or, for more space efficiency, you can use an empty struct as the value and use _, present = the_setoid[key]
to check for presence.
这篇关于golang为什么我们没有固定的数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!