function - 如何使用函数作为 map 的键

标签 function dictionary go key

如何使用函数作为 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/

相关文章:

javascript - 为什么或为什么不为某些数据类型的核心函数提供简写?

r - 如何使用函数通过ggplot2转换轴值?

winapi - syscall.Syscall 和 WinAPI 遇到问题

google-app-engine - 在 Go 中设置 App Engine 上下文

javascript - JS : Can I call a function with same scope as caller or do macro-like behavior

php - 如何调用自身内部的函数?

c - 如何在 C 中存储我的 map ?

c# - 使用字符串表达式 c# 将列表转换为字典

c++ - 查找和排序列表

python - 比 Python 慢?