复杂的数据类型作为Go中地图中的键 [英] Complex datatypes as keys in maps in Go

查看:37
本文介绍了复杂的数据类型作为Go中地图中的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Go中创建一个以大整数为键的地图.有效的Go明确表示:

I'm trying to create a map in Go that is keyed by big integers. Effective Go explicitly says that:

结构,数组和切片不能用作映射键,因为未在这些类型上定义相等性.

Structs, arrays and slices cannot be used as map keys, because equality is not defined on those types.

这很有道理.我当然可以将大整数转换为字符串,然后将字符串用作键,但是我在这里寻找一种更通用的解决方案.我可以将我的结构包装到实现相等函数的某些东西(接口吗?)中,而不是使用它?

which makes sense. I could of course convert the big integers to strings and use the string as a key, but I'm looking for a more general solution here. Can I wrap my structure into something (an interface?) that implements an equality function and use that instead?

当然不起作用的示例代码:

Example code that, of course, doesn't work:

package main                                                                                                           

import (                                                                                                               
        "big"                                                                                                          
        "fmt"                                                                                                          
)                                                                                                                      

func main() {                                                                                                          
        one1 := big.NewInt(1)                                                                                          
        one2 := big.NewInt(1)                                                                                          

        hmap := make(map[*big.Int] int)                                                                                
        hmap[one1] = 2                                                                                                 
        _, exists := hmap[one2]                                                                                        
        fmt.Printf("Exists=%v\n", exists)                                                                              
}                                                                                                                      

推荐答案

关于平等的规则即将更改.从执行1个计划:

The rules about equality are going to change soon. From the Go 1 plan:

Go 1将定义由以下组成的结构和数组值的相等性还定义了相等性的字段(逐元素比较).它将删除函数和映射值上的相等性,除了与零比较.第1行将继续排除以下情况的平等片.(在一般情况下是不可行的.)

Go 1 will define equality on struct and array values composed from fields on which equality is also defined (element-wise comparison). It will remove equality on function and map values except for comparison with nil. Go 1 will continue to exclude equality for slices. (In the general case it is infeasible.)

但是,即使有此规则,您也不能使用 * BigInt 作为键,因为它包含一个切片.还要注意,在Go中不可能编写自定义的相等运算符(也不能覆盖任何其他运算符).但这在我看来实际上是Go的优势-没有它,事情就更简单了.

However, even with this rules, you can not use *BigInt as key, since it contains a slice. Also note, that it is not possible in Go to write a custom equality operator (neither it is possible to override any other operator). But that's actually a strength of Go in my opinion - things are just simpler without it.

因此,您将必须使用字符串作为密钥.但是,只要您不想打印字符串,就不需要将其格式化为十进制(或任何其他格式).因此,最快的方法可能是使用 Bytes()方法(该方法也会丢弃符号,请确保将其单独包含在字符串中).

So, you will have to use strings for your keys. The strings however do not need to be formatted in decimal (or any other format) as long as you do not want to print them. So the fastest way is probably to use the Bytes() method (which will also discard the sign, make sure to include that separately in your string).

这篇关于复杂的数据类型作为Go中地图中的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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