Hi forum,
-
Go provides just two built-in data structures including slice and map. It has more than C (just array) but less than Python, C++ and Swift. I think this is great and makes everyone’s code kind of look familiar. But I still wonder what is the mind behind the design of the two member built-in data structures.
-
Why it does not require some kind of Hash or Equal interfaces, when I want to check if a key of type of custom struct is in a map? For example, if I’m going to sort slice of custom struct, I’ll need to define Len, Less, Swap of sort.
Thanks
package main
import (
"fmt"
)
type K struct {
name string
num int
}
func main() {
d := map[K]struct{}{}
d[K{"ccc", 3}] = struct{}{}
d[K{"bbb", 2}] = struct{}{}
d[K{"aaa", 1}] = struct{}{}
k := K{"bbb", 2}
v, ok := d[k]
fmt.Println(v, ok) //true
}