Maps in Golang

Tram Ho

Continuing my series of data structures in Go . Today, I will show you how to use Maps , and this is also the last article in the series of commonly used data structures that I want to introduce to you. You can review Part 1 Arrays and Part 2 Slices here.

1. Concepts

A map is a data structure that provides the programmer with an unordered collection of key / value pairs. You store values ​​in the map based on the key. The advantage of the map is its ability to get value quickly based on the key . The key works like an index , indicating the value being associated with the key .

2. Basic principles

Maps is a gallery, you can repeat it just like you would with arrays and slices . But maps are unordered collection so there is no way to predict the order in which key / value pairs will be returned. Even if you store key / value pairs in the same order, each time you iterate over the map it may get a different order. This happens because map implements hash table (you can learn more about hash table to understand. Details about hash table, I will write another article).

3. Declare and initialize

There are several ways to declare which maps are created in Go . You can use the make function or use map literal .

Using map literal is a way to create a new map . The length of the map will be based on the number of key / value pairs that you initialized when creating a new map . The key of the map can be a primitive type or a struct type defined by the user, but the == operator must be met. Slices, functions, and structs contain slices that cannot be used as map keys

4. Working with maps

Assigning a key / value pair to map is done by specifying a key and assigning a value to that key

You can also create a nil map by declaring map without initializing it. Note , a nil map cannot be used to store key / value pairs.

It is important to check whether a key already exists or not when using maps . It allows you to write logic to determine whether you have performed an operation or if you have stored values ​​in the map . It is also used to compare two maps to determine which key / value pairs match or are missing.

When getting values ​​from a map , you have two options. You can get a value and a flag indicating whether the key already exists or not.

Another option is that it just returns the value and checks that value to determine if the key exists or not. This can only work if the value 0 is not a valid value in the map .

When you access a map using the index in Go , it will always return a value, even if the key doesn’t exist. In this case, the value 0 is returned.

Loop on map is like looping on arrays or slices. You use the keyword range ; but for map, you cannot get the index / value. Instead, you will get key / value pair.

If you want to delete a key / value pair from a map , you use the delete function provided by Go .

This time, when you loop on the map, you will not see the Coral color on the screen.

5. Pass the map between functions

Passing a map between two functions will not create a copy of it. In fact, you could pass a map to a map modifier function that will be seen on all maps that reference it.

If you execute the above program, you will see the following output:

You can see, after the removeColor function was executed, the Coral color no longer exists in the map referenced in the main function. Maps is designed like slices in this point.

My article on Maps is quite brief. However, if you want to learn more, you can learn more techniques below, you can learn about hash tables . Thank you for reading my article. If you have any questions, don’t hesitate to leave a comment.

Link to the original article here, guys: https://chiasekienthuc.netlify.app/blog/maps

Share the news now

Source : Viblo