Sobes.tech
Junior — Middle

Millised omadused peavad võtmetel olema andmestruktuuris map?

sobes.tech AI

Vastus AI-lt

In Go, the keys in a map must be of a comparable type, meaning types that can be used with the == and != comparison operators. This is necessary for the map to efficiently search and compare keys.

Types that can be used as keys:

  • Simple types: strings, numbers, boolean values.
  • Pointers.
  • Structures, if all their fields are comparable.

Types that cannot be used as keys:

  • Slices, maps, functions, as they are not comparable.

Example:

m := make(map[string]int)
m["key"] = 42

Here, the key is a string, which is correct.