Sobes.tech
Junior — Middle

What is the purpose of the Map structure in Go language?

sobes.tech AI

Answer from AI

In the Go language, the Map structure is a built-in data type for storing key-value pairs. Maps allow for quick retrieval, addition, and deletion of elements by key.

The purpose of a Map is efficient storage and lookup of data by a unique key. Keys in a Map must be comparable types (e.g., strings, numbers), and values can be of any type.

Example of using a Map in Go:

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

fmt.Println(m["apple"]) // Will output 5

// Checking for the presence of a key
value, ok := m["orange"]
if !ok {
    fmt.Println("Key 'orange' is missing")
}

Maps are useful for implementing dictionaries, caches, and other data structures where fast access by key is important.

What is the purpose of the Map structure in Go… - sobes.tech