How to Master Go Maps in 10 Minutes

How to Master Go Maps in 10 Minutes

Hello ,

🔵 welcome to this blog post about maps in Go language. In this post, I will explain what maps are, how to create and use them, and some tips and tricks to make the most out of them. Let’s get started!

What are maps?

Maps are a collection of key-value pairs, where each key is associated with a value. Maps are also known as dictionaries, hashes, or associative arrays in other languages. Maps are useful for storing and retrieving data based on a unique identifier. For example, you can use a map to store the phone numbers of your contacts, where the key is the name of the contact and the value is the phone number.

How to create maps?

// Create an empty map of string keys and int values
m1 := make(map[string]int)

// Create a map of string keys and int values with initial capacity of 10
m2 := make(map[string]int, 10)

A map literal is a comma-separated list of key-value pairs enclosed in curly braces. You can use a map literal to create and initialize a map at the same time. For example:

// Create and initialize a map of string keys and int values
m3 := map[string]int{
    "Alice": 25,
    "Bob": 30,
    "Charlie": 35,
}

// DONOT USE MAKE FOR MAP LITERAL
NOTE:
The advantage of using the `make` function is that you can specify the initial capacity of the map, 
which can improve performance if you know the approximate number of elements in advance
but it will require more line of code to add elements

How to use maps?

You can use the square brackets notation to access, add, update, or delete elements from a map. The syntax is map[key], where key is an expression that evaluates to a valid key type. For example:

// Access the value associated with the key "Alice"
age := m3["Alice"] // age = 25

// Add a new key-value pair to the map
m3["David"] = 40

// Update the value associated with the key "Bob"
m3["Bob"] = 31

// Delete the key-value pair with the key "Charlie"
delete(m3, "Charlie")

How to check if a key exists in a map?

// Check if the key "Eve" exists in the map

age, isPresent := m3["Eve"]

// `age` is value associated with the key

// `isPresent` is a boolean that indicates whether the key was found or not

if isPresent {
    fmt.Println("Eve's age is", age)
} else {
    fmt.Println("Eve is not in the map")
}

Tips and tricks

Here are some tips and tricks to make the most out of maps in Go:

  • You can use the len function to get the number of elements in a map. For example: fmt.Println(len(m3))

  • You can use the range keyword to iterate over a map. The range expression returns two values: the key and the value for each element in the map. You can use the blank identifier _ to ignore either value if you don’t need it. For example:

// Print all key-value pairs in the map
for k, v := range m3 {
    fmt.Println(k, v)
}

// Print only keys in the map
for k := range m3 {
    fmt.Println(k)
}

// Print only values in the map
for _, v := range m3 {
    fmt.Println(v)
}
  • You can use a struct type as a value type in a map to store more complex data. For example:
// Define a struct type for person information
type Person struct {
    name string
    age int
    phone string
}

// Create a map of string keys and person values
m4 := map[string]Person{
    "Alice": Person{"Alice", 25, "111-2222"},
    "Bob": Person{"Bob", 30, "333-4444"},
}

// Access a field of a person value in the map
fmt.Println(m4["Alice"].phone) // 111-2222
  • You can use a slice type as a value type in a map to store multiple values for each key. For example:
// Create a map of string keys and slice of string values
m5 := map[string][]string{
    "Alice": []string{"apple", "banana", "cherry"},
    "Bob": []string{"orange", "pear", "grape"},
}

// Access an element of a slice value in the map
fmt.Println(m5["Bob"][1]) // pear

// Append a new element to a slice value in the map
m5["Alice"] = append(m5["Alice"], "date")
  • You can use another map type as a value type in a map to create nested maps. For example:
// Create a map of string keys and map of string keys and int values
m6 := map[string]map[string]int{
    "Alice": map[string]int{
        "Math": 90,
        "English": 80,
        "Science": 85,
    },
    "Bob": map[string]int{
        "Math": 70,
        "English": 75,
        "Science": 80,
    },
}

// Access an element of a nested map value in the main map
fmt.Println(m6["Alice"]["Science"]) // 85

// Update an element of a nested map value in the main map
m6["Bob"]["Math"] = 72

Conclusion

Maps are one of the most powerful and versatile data structures in Go, and they can help you solve many common problems in programming. In this post, we learned what maps are, how to create and use them, and some tips and tricks to make the most out of them. I hope you enjoyed this post and learned something new. If you have any questions or feedback, please feel free to leave them in the comments section below. Thank you for reading! 😊