Maps are Go's built-in hash tables, offering efficient key-value storage and retrieval. They are a fundamental data structure for fast lookups and are used extensively in Go programs. Understanding how to initialize, use, and apply common patterns with maps is essential for effective Go development.
Maps in Go are initialized using the make
function or by using a map literal.
go// Using make function
m1 := make(map[string]int)
// Using map literal
m2 := map[string]int{"foo": 1, "bar": 2}
make
function initializes an empty map.Elements in a map are added and accessed using the key.
gom := make(map[string]int)
m["key1"] = 10 // Adding an element
value := m["key1"] // Accessing an element
fmt.Println(value) // Output: 10
To check if a key exists in a map, use the two-value assignment form.
govalue, exists := m["key1"]
if exists {
fmt.Println("Key exists with value:", value)
} else {
fmt.Println("Key does not exist")
}
exists
) is a boolean indicating if the key was found.The delete
function removes a key-value pair from a map.
gom := map[string]int{"foo": 1, "bar": 2}
delete(m, "foo")
fmt.Println(m) // Output: map[bar:2]
delete(m, "foo")
removes the key "foo"
and its associated value from the map.Use a for
loop with the range
keyword to iterate over a map.
gom := map[string]int{"foo": 1, "bar": 2}
for key, value := range m {
fmt.Printf("%s: %d\n", key, value)
}
Go maps can use any type that is comparable (supports ==
and !=
) as keys.
gotype Person struct {
FirstName string
LastName string
}
m := make(map[Person]int)
p := Person{"John", "Doe"}
m[p] = 30
fmt.Println(m) // Output: map[{John Doe}:30]
Values in a map can be of any type, including slices, structs, and even other maps.
gotype Employee struct {
ID int
Name string
Salary float64
}
employeeMap := make(map[string]Employee)
employeeMap["e123"] = Employee{ID: 123, Name: "Alice", Salary: 50000}
fmt.Println(employeeMap)
Maps of maps are useful for representing multi-dimensional associative arrays.
gonestedMap := make(map[string]map[string]int)
nestedMap["category1"] = map[string]int{"item1": 10, "item2": 20}
nestedMap["category2"] = map[string]int{"item3": 30, "item4": 40}
fmt.Println(nestedMap) // Output: map[category1:map[item1:10 item2:20] category2:map[item3:30 item4:40]]
When accessing a map, if a key does not exist, the zero value for the map's value type is returned.
gom := map[string]int{"foo": 1}
fmt.Println(m["bar"]) // Output: 0 (zero value for int)
make
or map literals to avoid nil map errors.Maps in Go are powerful tools for managing key-value pairs efficiently. Understanding how to initialize and manipulate maps, handle complex key and value types, and apply common patterns for nested maps and default values will enhance your ability to write effective and efficient Go programs. By adhering to best practices, you can avoid common pitfalls and leverage the full potential of Go maps in your applications.