5 ms·
Don't all the keys in a Go map have to have the same type? So if you use structs as keys, they all need to have the same length? In Python, you can use (1, 2),
by bakery2k 7y ago
Don't all the keys in a Go map have to have the same type? So if you use structs as keys, they all need to have the same length?
In Python, you can use (1, 2), (3, 4, 5) and (6, 7, 8, 9, 10) all as keys in the same map.
- tapirl 7y agoSame in Go, you can use interface{} values as keys, the concrete values of the interface keys can be a 2-element, 3-element or n-element array values. package main import "fmt" func main() { var m = map[interface{}]int { [2]int{1, 2}: 12, [3]int{1, 2, 4}: 123, [4]int{1, 2, 3, 4}: 1234, } fmt.Println(m) }
- omaranto 7y agoOh, cool! This still doesn't quite get rid of my complaint since the size of an array must be known at compile time, but it's good that they don't all have to be the same size.