How to use function as map's key? for example:
type Action func(int)
func test(a int) { }
func test2(a int) { }
func main() {
x := map[Action]bool{}
x[test] = true
x[test2] = false
}
those code would show an error: invalid map key type Action
A Map 's keys can be any value (including functions, objects, or any primitive). The keys of an Object must be either a String or a Symbol . The keys in Map are ordered in a simple, straightforward way: A Map object iterates entries, keys, and values in the order of entry insertion.
One solution is to build a hash table that will look like: { key1: Array('all values of key1'), key2: Array('all values of key2'), ...} Then use this hash table to get result quickly.
To get the keys of a Map object, you use the keys() method. The keys() returns a new iterator object that contains the keys of elements in the map.
To get value for a specific key in Map in JavaScript, call get() method on this Map and pass the specific key as argument. get() method returns the corresponding value for given key, if present. If the specified key is not present, then get() returns undefined.
You cannot use a function as a map key. The language specification clearly says:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With