A Guide to Golang’s Maps

In our previous blogs, we’ve discussed Golang’s data types, arrays and slices. In this tutorial, we present a one more in-built type—the maps. By the end of the class, you should have learnt how to use maps in your Golang codes.

Are you ready?

A map is an unordered combination of key-value pairs that helps to look-up values by using their associated keys. You should recall that a hash table is one of the most important data structure that programmers use to look-up values using associated keys. The merits of using a hash table in a program are that it helps in providing fast lookups, additions, and deletes.

Well, Golang provides an in-built map data type that contributes to implementing a hash table. So, let’s dive in and find out how you can implement a hash table using Golang.

How to declare and initialize Golang maps
Golang maps are declared using the syntax below:

var n map[string]int

In this declaration, the map data type is specified by the keyword “map” which is followed by the key data type in brackets and finally the value type. Such a declaration can be read as “n is a map of strings to integers.” You can figure this as a hash table that can help you provide fast look-ups using the keys (indexes) through referencing.

The map types will act as reference types, just like the pointers or slices. Therefore, the value of n above is set to “nil” since it hasn’t been initialized. Nil maps are empty when they are read.

Maps are initialized using the in-built make function. Here’s an example that initializes a map n that has been declared above:

n = make (map [string] int)

The main function of the make function is to allocate and initialize the hash map data structure. It also returns the map value that points to the map.

Here’s what you should note about Golang’s maps:

    • •They aren’t good when you use them for concurrent use. If you want to read and write to a map at the same time, the accesses must be moderated by synchronization mechanisms such as the sync.RWMutex.

 

    •The map keys that you specify under the declaration can be of any data type that is comparable. For instance, you can use Boolean, numeric or string data types that you want in your maps.

So, how can work with maps in your Golang code? Keep reading.

How to work with Maps in Golang
Just like arrays and slices, maps can be accessed using the brackets. Here’s an example:

var n map [string]int
n ["key"] = 100
fmt.Println (n ["key"])

You can also create maps using the key type of integer. Here’s an example:

n: = make (map [int] int)
n [1] = 200
fmt.Println (n [1])

In the above example, the map has been declared and initialized just like Golang arrays.

Golang Map Functions
Here are examples of Golang map functions:

    • 1.

The delete () function

    • It’s used for deleting entries in Golang maps. For you to delete an entry from a map, you need to have the map and the corresponding key that should be deleted. Here’s an example that shows how to use the delete function if you want to delete an entry from a map:
delete (n,"Yaoundé")
fmt.Println (" The entry for Yaoundé has been deleted")  
fmt.Println ("The map has been updated after the delete process")  

2.The len () function
It’s used to specify the number of items on a map. Here’s an example of Golang code that uses the len () function:

n: = make (map [int] int)
n [1] = 200
x := len(n)
fmt.Println (n [1])
fmt.Println (“The length of this map is:”, x)

You can now begin using maps when writing Golang maps.

GET YOUR FREE Golang EBOOK!

(Visited 1,377 times, 1 visits today)