I've question regarding maps
1. The article says maps take the interface{} for key and value.
Consider this example
""
package main¬
¬
import (¬
» "fmt"¬
)¬
¬
type Woot struct {¬
» a,b,c int¬
}¬
¬
func main() {¬
¬
» a := make(map[interface{}]interface{})¬
¬
» a[Woot{1,2,3}] = true¬
¬
» fmt.Println(a)¬
}
""
Maps cannot take types which are not "hashable", which makes perfect sense.
I would expect it to require a "Hashable" interface so that I can create a
type which provides the hashing function. Why is that way?
-----
2. And secondly how would you implement a compound key. I.e. indexing with
more than one variable? (Best practice so to speak)
This is a kind of magic that Go shies away from. If your type has a
Hash function that you want to use to generate the hashes, just call
it when writing or reading from the map:
m := make(map[string]interface{})
m[key.Hash()] = value
value = m[key.Hash()]
This has been discussed before. Search these groups for hashable or
hash map for more.
> 2. And secondly how would you implement a compound key. I.e. indexing with
> more than one variable? (Best practice so to speak)
You could maintain two maps. I'm not sure what you mean specifically, though.
Andrew