Junior — Middle
What functions and advantages does a hash table (map) data structure provide?
sobes.tech AI
Answer from AI
A hash table (map) in Go provides a convenient structure for storing key-value pairs with fast access by key. Main functions and advantages:
- Fast access: Search, insert, and delete operations are on average O(1).
- Flexible keys: Keys can be types that support comparison (e.g., strings, numbers).
- Dynamic resizing: Map automatically grows when new elements are added.
- Ease of use: Syntax for working with maps is intuitive.
Example:
m := make(map[string]int)
m["apple"] = 5
fmt.Println(m["apple"]) // prints 5
// Check if key exists
value, ok := m["banana"]
if !ok {
fmt.Println("Key 'banana' does not exist")
}