Sobes.tech
Junior — Middle

What types of keys are possible to use in a Map data structure?

sobes.tech AI

Answer from AI

In Go, keys for the map data structure can be any types that support comparison with == and !=. This means keys can be:

  • Boolean values (bool)
  • Numeric types (int, float64, etc.)
  • Strings (string)
  • Pointers
  • Structs, if all their fields are comparable

Slices (slice), maps (map), and functions cannot be used as keys because they are not comparable.

Example of using a map with a string key:

m := make(map[string]int)
m["apple"] = 5
fmt.Println(m["apple"])

If you need to use complex keys, you can define a struct with comparable fields.

What types of keys are possible to use in a Map data… - sobes.tech