如何使用函数作为 map 的键?例如:
type Action func(int)
func test(a int) { }
func test2(a int) { }
func main() {
x := map[Action]bool{}
x[test] = true
x[test2] = false
}
这些代码会显示错误:invalid map key type Action
最佳答案
您不能将函数用作映射键。 language specification明确地说:
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.
关于function - 如何使用函数作为 map 的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27267042/