Junior — Middle
In what scenarios have you used the Map data structure in your projects?
sobes.tech AI
Answer from AI
In Go, the data structure Map is often used for quick access to data by key. In projects, I have used Map in the following scenarios:
- Caching the results of computations or queries to speed up repeated access.
- Storing configuration parameters, where the key is the parameter name, and the value is its value.
- Counting the number of occurrences of elements (for example, words in a text).
- Associating identifiers with objects or structures for quick lookup.
Example of counting the number of words:
words := []string{"apple", "banana", "apple", "orange", "banana", "apple"}
count := make(map[string]int)
for _, word := range words {
count[word]++
}
fmt.Println(count) // map[apple:3 banana:2 orange:1]