Golang Basics: Maps

Nikhil Vaidyar
3 min readJul 4, 2021
Not actually a physical form of maps
Not a physical form of maps

Hey everyone, Today, I will talk about the concept of maps in Go, which is used to store data in key values pairs. A brief about the Go maps is that built-in map types are implemented by a common data structure, hash table.

What is Hash Table?

A hash table is a data structure where data is stored in an associative manner. The data mapped to array positions by a hash functions that generates a unique value from each key.

Hash Table representation
Hash Table representation

The major advantage of hash tables that they are efficient. The access time for an element is on average O(1), so it offers faster lookups, adds, and deletes. The hash function generates and addresses from the key. This process of mapping keys to the appropriate location is called hashing.

Okay, enough explanation of hash table data structure. Coming back to our topic. So, map data type in Go means it maps keys to values. It maps keys to values and stores the data in Go.

Maps in action

Here’s how a Go to map data type looks like

Map signature in Go

Map types are just reference types like slices or pointers. An empty map acts as a nil data type in Go, whereas if you just declared a nil map, it will return an empty map like this:

var m map[string]int
// map[]

If we try to write to a nil map, it will cause the runtime panic error. Here’s the screenshot of that:

Runtime panic error of nil maps in Go
Runtime panic error of nil maps in Go

You can also check this out just go to this link: https://play.golang.org/. Go provides a playground to run, test, or multiple operations, etc.

In this case, as we have declared a map using the var keyword, we can use a built-in function make to pass this runtime panic error. While using this function it allocates and initializes a hash map data structure and returns a map value that points to it.

var m map[string]int
m = make(map[string]int)
m["age"] = 24
fmt.Println(m) // map[age:24]

Hence, we can also initialize a map using map literal.

m := map[string]string{"name": "John", "age": "24", "sex": "Male"}
// map[age:24 name:John sex:Male]

We can also initialize an empty array with map literals, which is identical to use the built-in make function.

To iterate over key-value pairs in the map, we can use the range keyword which we have discussed in our previous blog of Array and Slices

Range keyword in Go map
Range keyword in Go map

There are two other built-in functions that Go provides us to execute more operations in our code, we can use len to check the map length, and also if we want to delete some of the existing keys, we can just implement the delete function while passing the values in arguments. Moreover, the delete function doesn’t return anything and will do nothing if the specified key doesn’t exist. Hence, I am trying these two built-in functions for the above mapData map.

len(mapData) // 3
delete(mapData, "name") // map[age:812 location:Titan]

Concurrency in maps

According to Go, maps are not safe for concurrent use, because if we need to read from and write to a map from concurrently executing goroutines, we need to perform this operation in a more synchronized manner. Thus, Go provides us a common way to handle and secure maps using sync.RWMutex.

sync.RWMutex

It’s a reader/writer mutual exclusion lock. It contains filtered data. This is a deep topic which I will explain thoroughly in my upcoming Golang series blogs.

--

--

Nikhil Vaidyar

Maintaining consistency | CNCF Contributor | Golang | Docker | Kubernetes