In Go, a map is an unordered collection of key-value pairs. Maps are reference types, which means that when you assign one map to another, both variables point to the same underlying data structure. This makes maps highly efficient for lookups, insertions, and deletions.
Maps are particularly useful when you need to associate unique keys with values. For example, you might use a map to store user IDs as keys and their corresponding usernames as values.
A map in Go is declared using the map keyword followed by the key type and value type in square brackets. Here's the basic syntax:
var myMap map[keyType]valueType
You can also initialize a map with make function:
myMap := make(map[keyType]valueType)
Maps are not initialized by default, so you need to use make or assign a pre-initialized map.
Let's create a simple map that associates integer keys with string values:
1package main23import "fmt"45func main() {6// Declare and initialize a map7myMap := make(map[int]string)89// Assign values to the map10myMap[1] = "Alice"11myMap[2] = "Bob"12myMap[3] = "Charlie"1314fmt.Println(myMap)15}
map[1:Alice 2:Bob 3:Charlie]
You can access the value associated with a key using the syntax myMap[key]. If the key does not exist, it returns the zero value of the value type.
1package main23import "fmt"45func main() {6myMap := map[int]string{71: "Alice",82: "Bob",93: "Charlie",10}1112// Accessing a value13fmt.Println(myMap[2]) // Output: Bob1415// Accessing a non-existent key16fmt.Println(myMap[4]) // Output: (empty string)17}
Bob
To check if a key exists in the map, you can use a two-value assignment:
value, exists := myMap[key]
If exists is true, then value contains the value associated with the key. If exists is false, then key does not exist in the map.
1package main23import "fmt"45func main() {6myMap := map[int]string{71: "Alice",82: "Bob",93: "Charlie",10}1112value, exists := myMap[2]13if exists {14fmt.Println("Key exists:", value)15} else {16fmt.Println("Key does not exist")17}18}
Key exists: Bob
You can delete a key-value pair from the map using the delete function:
delete(myMap, key)
This removes the key and its associated value from the map.
1package main23import "fmt"45func main() {6myMap := map[int]string{71: "Alice",82: "Bob",93: "Charlie",10}1112delete(myMap, 2)13fmt.Println(myMap) // Output: map[1:Alice 3:Charlie]14}
map[1:Alice 3:Charlie]
You can iterate over the key-value pairs in a map using a for loop with the range keyword:
1package main23import "fmt"45func main() {6myMap := map[int]string{71: "Alice",82: "Bob",93: "Charlie",10}1112for key, value := range myMap {13fmt.Printf("Key: %d, Value: %s14", key, value)15}16}
Key: 1, Value: Alice Key: 2, Value: Bob Key: 3, Value: Charlie
You can also initialize a map using a map literal:
myMap := map[keyType]valueType{
key1: value1,
key2: value2,
// ...
}
<CodeBlock language="go">
{`package main
import "fmt"
func main() {
myMap := map[int]string{
1: "Alice",
2: "Bob",
3: "Charlie",
}
fmt.Println(myMap)
}`}
</CodeBlock>
<OutputBlock>
{`map[1:Alice 2:Bob 3:Charlie]`}
</OutputBlock>
## What's Next?
In the next section, we will explore structs in Go. Structs are composite data types that allow you to group multiple fields together into a single type.
Stay tuned for more insights into Go's powerful features!