A Comprehensive Guide to Maps in Go

Isuru Harischandra
Towards Dev
Published in
4 min readApr 21, 2023

--

Photo by Annie Spratt on Unsplash

Introduction

Maps are one of the most commonly used data structures in programming. They provide a way to associate a key with a value, allowing efficient lookup and modification of data. In Go, maps are built-in associative data types, also known as hashes or dicts in other programming languages. In this article, we will dive deep into maps in Go, covering their creation, manipulation, and usage.

Creating a Map

Photo by GeoJango Maps on Unsplash

To create an empty map in Go, we use the built-in makefunction with the syntax make(map[key-type]val-type). Here, key-type specifies the type of key and val-type specifies the type of the value. For example, let’s create an empty map with string keys and int values:

m := make(map[string]int)

Adding and Accessing Key-Value Pairs

To add a key-value pair to a map, we use the syntax name[key] = val. For example, to add the key-value pair "apple": 3 to our map m, we write:

m["apple"] = 3

We can access the value associated with a key using the syntax name[key]. For example, to get the value associated with the key "apple" in our map m, we write:

fmt.Println(m["apple"]) // Output: 3

If the key doesn’t exist in the map, the zero value of the value type is returned. For example, if we try to access the value associated with the key "banana" in our map m, we get:

fmt.Println(m["banana"]) // Output: 0

Getting the Number of Key-Value Pairs

Photo by regularguy.eth on Unsplash

To get the number of key-value pairs in a map, we use the built-in len function. For example, to get the number of key-value pairs in our map m, we write:

fmt.Println(len(m)) // Output: 1

Deleting Key-Value Pairs

To remove a key-value pair from a map, we use the built-in delete function with the syntax delete(name, key). For example, to remove the key-value pair "apple": 3 from our map m, we write:

delete(m, "apple")

Iterating over a Map

Photo by Crissy Jarvis on Unsplash

We can iterate over a map using a for loop. Each iteration of the loop returns a key-value pair as a tuple. We can use the syntax name, _ := range m to get only the keys or name, value := range m to get both the keys and the values. For example, let’s print all the key-value pairs in our map m:

for k, v := range m {
fmt.Println(k, v)
}

Declaring and Initializing a Map

In Go, we can also declare and initialize a new map in the same line using the syntax name := map[key-type]val-type{k1: v1, k2: v2, ...}. For example, to create a map with string keys and string values, initialized with two key-value pairs, we write:

fruits := map[string]string{"apple": "red", "banana": "yellow"}

Printing a Map

When we print a map using fmt.Println, it is displayed in the form map[k: v, k: v]. For example, if we print our map fruits, we get:

fmt.Println(fruits) // Output: map[apple:red banana:yellow]

Conclusion

Maps are powerful data structures in Go that allow us to associate a key with a value. In this article, we covered how to create an empty map, add and access key and value pairs, get the number of key-value pairs, delete key-value pairs, iterate over a map, declare and initialize a map in the same line, and print a map. By understanding these concepts, you can efficiently use maps in your Go programs and make your code more readable and maintainable.

Overall, maps are a fundamental data structure in Go, and mastering them is essential for any Go programmer. I hope this article has been informative and useful in your journey to becoming a skilled Go developer.

Happy coding!

--

--

COO of Bitzquad | BSc. (Hons) in Information Systems - Reading | Final Year Undergraduate in University of Colombo School of Computing