Go中的结构集 [英] Set of structs in Go

查看:142
本文介绍了Go中的结构集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 类型Stuff struct {
a string
b string
}

我可以用切片来做,但它看起来像它会使用更少的内存来使用适当的设置结构。



不幸的是,Go没有设置结构。每个人都建议使用 map [Stuff] struct {} 但这不起作用,因为 Stuff 是一个结构体。任何人有什么好的解决方案理想情况下,无需下载库。

解决方案

通常,set和map数据结构需要更多的内存,而不是存储纯数值列表数组或切片作为集合和映射提供了附加特性,例如按键的唯一性或值检索。



如果您想要最小的内存使用量,只需将它们存储在切片中,例如 []东西。如果你在多个地方使用这些值,那么只存储它们的指针也是有利的。 [] * Stuff ,因此每个存储相同的 Stuff 值的地方可以存储相同的指针(不重复该值)。

如果您只想存储唯一的结构值,那么该集合将是最方便的选择,Go通过映射实现。



map [Stuff] struct {} 没有问题, 。



关键类型的要求


比较运算符 ==和!=必须完整为键类型的操作数定义;因此键类型不能是函数,映射或切片。


东西是一个结构体,而 Go中的结构体具有可比性


如果所有字段都具有可比性,则结构值具有可比性。如果其相应的非空白字段相同,则两个结构值相等。


如果您的 Stuff 结构是您发布的结构,则它具有可比性:它只包含可比键入 string



另外请注意,如果你想要一个集合数据结构,如果你使用 bool 作为值类型(例如 map [Stuff] bool )和 true 作为值,然后您可以简单地使用索引来测试值是否在地图中因为索引表达式为值类型(如果关键字<<$ c> bool )则产生值类型的零值( false $ c> Stuff 在你的案例中)不在地图中,正确地告诉你正在查找的值不在set中。 (如果它在地图中,它的关联 true 值是索引表达式的结果 - 正确地告诉它在地图中)。


If I have a number of structs that I want to store:

type Stuff struct {
    a string
    b string
}

I can do it with a slice, but it seems like it would use less memory to use a proper set structure.

Unfortunately Go doesn't have a set structure. Everyone recommends using map[Stuff]struct{} but that doesn't work because Stuff is a struct. Anyone have any good solutions? Ideally without having to download a library.

解决方案

Usually set and map data structures require more memory than storing a list of values in plain array or slice as set and map provide additional features efficiently like uniqueness or value retrieval by key.

If you want minimal memory usage, simply store them in a slice, e.g. []Stuff. If you use the values in multiple places, it might also be profitable to just store their pointers, e.g. []*Stuff and so each places that store the same Stuff values can store the same pointer (without duplicating the value).

If you only want to store unique struct values, then indeed the set would be the most convenient choice, in Go realized with a map.

There's nothing wrong with map[Stuff]struct{}, it works. The requirement for the key type for maps:

The comparison operators == and != must be fully defined for operands of the key type; thus the key type must not be a function, map, or slice.

Stuff is a struct, and structs in Go are comparable if:

Struct values are comparable if all their fields are comparable. Two struct values are equal if their corresponding non-blank fields are equal.

If your Stuff struct is what you posted, it is comparable: it only contains fields of the comparable type string.

Also note that if you want a set data structure, it's clearer if you use bool as the value type (e.g. map[Stuff]bool) and true as the value, and then you can simply use indexing to test if a value is in the map as the index expression yields the zero value of the value type (false for bool) if the key (Stuff in your case) is not in the map, properly telling the value you're looking for is not in the "set". (And if it is in the map, its associated true value is the result of the index expression - properly telling it is in the map).

这篇关于Go中的结构集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆