在 golang 中使用变长数组作为映射键

我找不到这样做的好方法。我想从排序的键值对列表中获取映射。


type Tag struct {

  key   string

  value string

}


type SortedTag []Tag // sorted list of tags.

map[SortedTags]T // cannot do.

我可以通过用分隔符连接所有键值对来解决这个问题,但我觉得这在很多方面效率低下且容易出错。转换回键值对很麻烦,因为我们需要拆分输入。此外,如果键值对可以是任何东西,那意味着我们必须逃避它。


如果它是 python,我会存储Tag为排序的 2-tupless 的 N 元组。


如果它是 java,我会创建一个复合对象,Map<String,String>并equals()检查另一个哈希映射,hashCode()返回映射xor的所有哈希值(xor因为它是可交换的,因此我们可以以任何顺序迭代映射来计算这个值) .


在go中,我想不出任何其他的好方法。


翻翻过去那场雪
浏览 318回答 2
2回答

天涯尽头无女友

例如,package mainimport "fmt"type Tag struct {&nbsp; &nbsp; Key&nbsp; &nbsp;string&nbsp; &nbsp; Value string}type Tags []Tagtype TagsValue struct {&nbsp; &nbsp; // some type used as Tags value}type TagsMapValue struct {&nbsp; &nbsp; Tags&nbsp; &nbsp; TagsValue}type TagsMapKey stringtype TagsMap map[TagsMapKey]TagsMapValuefunc NewTagsMapKey(tags Tags) TagsMapKey {&nbsp; &nbsp; b := []byte{}&nbsp; &nbsp; for _, tag := range tags {&nbsp; &nbsp; &nbsp; &nbsp; b = append(b, tag.Key...)&nbsp; &nbsp; &nbsp; &nbsp; b = append(b, tag.Value...)&nbsp; &nbsp; }&nbsp; &nbsp; return TagsMapKey(b[:len(b)])}func (m *TagsMap) AddElement(tags Tags, tagsValue TagsValue) {&nbsp; &nbsp; mapKey := NewTagsMapKey(tags)&nbsp; &nbsp; mapValue := TagsMapValue{Tags: make(Tags, 0, len(tags)), TagsValue: tagsValue}&nbsp; &nbsp; i := 0&nbsp; &nbsp; for _, tag := range tags {&nbsp; &nbsp; &nbsp; &nbsp; key := string(mapKey[i : i+len(tag.Key)])&nbsp; &nbsp; &nbsp; &nbsp; i += len(tag.Key)&nbsp; &nbsp; &nbsp; &nbsp; value := string(mapKey[i : i+len(tag.Value)])&nbsp; &nbsp; &nbsp; &nbsp; i += len(tag.Value)&nbsp; &nbsp; &nbsp; &nbsp; mapValue.Tags = append(mapValue.Tags, Tag{Key: key, Value: value})&nbsp; &nbsp; }&nbsp; &nbsp; (*m)[mapKey] = mapValue&nbsp; &nbsp; return}func main() {&nbsp; &nbsp; m := make(TagsMap)&nbsp; &nbsp; sortedTags := Tags{&nbsp; &nbsp; &nbsp; &nbsp; {Key: "key1", Value: "value1"},&nbsp; &nbsp; &nbsp; &nbsp; {Key: "key7", Value: "value7"},&nbsp; &nbsp; &nbsp; &nbsp; {Key: "key7", Value: "value49"},&nbsp; &nbsp; &nbsp; &nbsp; {Key: "key42", Value: "value42"},&nbsp; &nbsp; }&nbsp; &nbsp; m.AddElement(sortedTags, TagsValue{})&nbsp; &nbsp; for k, v := range m {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Tags Key:", k)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("&nbsp; &nbsp;Tags:&nbsp; &nbsp; &nbsp; ", v.Tags)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("&nbsp; &nbsp;Tags Value:", v.TagsValue)&nbsp; &nbsp; }}输出:Tags Key: key1value1key7value7key7value49key42value42&nbsp; &nbsp;Tags:&nbsp; &nbsp; &nbsp; &nbsp;[{key1 value1} {key7 value7} {key7 value49} {key42 value42}]&nbsp; &nbsp;Tags Value: {}如果您只是想测试标签集成员资格,package mainimport "fmt"type Tag struct {&nbsp; &nbsp; Key&nbsp; &nbsp;string&nbsp; &nbsp; Value string}type Tags []Tagtype TagsSetKey stringtype TagsSet map[TagsSetKey]Tagsfunc NewTagsSetKey(tags Tags) TagsSetKey {&nbsp; &nbsp; b := []byte{}&nbsp; &nbsp; for _, tag := range tags {&nbsp; &nbsp; &nbsp; &nbsp; b = append(b, tag.Key...)&nbsp; &nbsp; &nbsp; &nbsp; b = append(b, tag.Value...)&nbsp; &nbsp; }&nbsp; &nbsp; return TagsSetKey(b[:len(b)])}func (m *TagsSet) AddElement(tags Tags) {&nbsp; &nbsp; setKey := NewTagsSetKey(tags)&nbsp; &nbsp; setValue := make(Tags, 0, len(tags))&nbsp; &nbsp; i := 0&nbsp; &nbsp; for _, tag := range tags {&nbsp; &nbsp; &nbsp; &nbsp; key := string(setKey[i : i+len(tag.Key)])&nbsp; &nbsp; &nbsp; &nbsp; i += len(tag.Key)&nbsp; &nbsp; &nbsp; &nbsp; value := string(setKey[i : i+len(tag.Value)])&nbsp; &nbsp; &nbsp; &nbsp; i += len(tag.Value)&nbsp; &nbsp; &nbsp; &nbsp; setValue = append(setValue, Tag{Key: key, Value: value})&nbsp; &nbsp; }&nbsp; &nbsp; (*m)[setKey] = setValue&nbsp; &nbsp; return}func (m *TagsSet) IsMember(tags Tags) bool {&nbsp; &nbsp; return (*m)[NewTagsSetKey(tags)] != nil}func main() {&nbsp; &nbsp; m := make(TagsSet)&nbsp; &nbsp; sortedTags := Tags{&nbsp; &nbsp; &nbsp; &nbsp; {Key: "key1", Value: "value1"},&nbsp; &nbsp; &nbsp; &nbsp; {Key: "key7", Value: "value7"},&nbsp; &nbsp; &nbsp; &nbsp; {Key: "key7", Value: "value49"},&nbsp; &nbsp; &nbsp; &nbsp; {Key: "key42", Value: "value42"},&nbsp; &nbsp; }&nbsp; &nbsp; m.AddElement(sortedTags)&nbsp; &nbsp; for k, v := range m {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Tags Key:", k)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("&nbsp; &nbsp;Tags: ", v)&nbsp; &nbsp; }&nbsp; &nbsp; // In set&nbsp; &nbsp; fmt.Println(m.IsMember(sortedTags))&nbsp; &nbsp; // Not in set&nbsp; &nbsp; sortedTags[0].Key = "key0"&nbsp; &nbsp; fmt.Println(m.IsMember(sortedTags))}输出:Tags Key: key1value1key7value7key7value49key42value42&nbsp; &nbsp;Tags:&nbsp; [{key1 value1} {key7 value7} {key7 value49} {key42 value42}]truefalse

慕的地6264312

如果你在(排序的)元组之后,你可以查看kmanley/golang-tuple它确实有排序元组的例子。这与dekarep/golang-set 不同,后者也有助于管理这些Tag.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go