Sobes.tech
Senior

Explain what a map is in Go and how you understand it.

sobes.tech AI

Answer from AI

In Go, a map is a built-in data type that implements a hash table, storing key-value pairs. It allows efficient storage and quick retrieval of data by key. Keys in a map must be comparable types (e.g., strings, numbers, pointers), and values can be of any type.

Main features:

  • Initialized using make or a literal.
  • Access to elements via map[key].
  • Accessing a non-existent key returns the zero value of the type.
  • You can check for the presence of a key using the second return value.

Example:

m := make(map[string]int)
m["apple"] = 5
value, ok := m["apple"]
if ok {
    fmt.Println("Value for 'apple':", value)
}
Explain what a map is in Go and how you understand… - sobes.tech