I have a struct that contains a slice and I would like to use it as a key to a map. I understand that this is not allowed because equality is not currently defined for slices in Go. I also know that I can't override equality for the struct to do the slice comparison by hand. My question is: what is the most idiomatic way to accomplish what I'm trying to do here?
This is a bit of example code that makes the structure more clear:
package main
import "fmt"
type InternalStruct struct {
item1, item2 bool
}
type ContainerStruct struct {
internals []InternalStruct
}
func main() {
container1 := ContainerStruct{}
container1.internals = append(container1.internals, InternalStruct{item1: true})
container2 := ContainerStruct{}
container2.internals = append(container2.internals, InternalStruct{item1: true})
m := make(map[ContainerStruct]int)
m[container1] = 10
fmt.Printf("container1 maps to: %d
", m[container1])
fmt.Printf("container1 maps to: %d
", m[container2])
}
This code doesn't compile (as expected), but I'm looking for something equivalent that would output "10" twice. What's the best way to go about achieving this result?