Sobes.tech
Middle+

Tell me how the map is organized in Go?

sobes.tech AI

Answer from AI

In Go, a map is a built-in data type for storing key-value pairs with fast access.

Structure:

  • map is implemented as a hash table.
  • Keys are hashed to determine the index in the bucket array.
  • Each bucket contains multiple key-value pairs to resolve collisions.
  • When the number of elements grows, re-hashing occurs, and the table size increases.

Features:

  • Keys must be comparable (e.g., strings, numbers).
  • Access to elements is amortized O(1).
  • Iteration over a map does not guarantee order.

Example:

m := make(map[string]int)
"apple" := 5
value := m["apple"]