Sobes.tech
Senior

Milliseid andmetüüpe saab kasutada kaardi võtmena? Kas võib kasutada massiivi võtmena?

sobes.tech AI

Vastus AI-lt

In Go, the key for a map can be any type that supports the comparison operations == and !=. This means keys can be basic types (int, string, bool, etc.), pointers, structures if all their fields are comparable, and interfaces if the dynamic type is comparable.

Arrays in Go are comparable if their elements are comparable, so arrays can be used as keys in a map. For example:

m := make(map[[3]int]string)
 m[[3]int{1, 2, 3}] = "value"

However, slices cannot be used as keys because they are not comparable (except when compared with nil).