Maps are a very powerful form of data in many programming languages. JSON or python dictionaries are good examples of maps. Maps in Go are a type of associative array as well. Associative arrays can be extremely good for data that lends itself to manipulation and queries. In today’s post, we will be looking at the various ways you can implement maps in Go.

Making Maps in Go

A map in go takes on a simple format: map[key]value. Unlike an array or slice in Go, maps allow you to choose two different types; one for the index or key and one for the value. For the key types, we can use any type that is comparable. This means that our keys can be of any type that can be used in a conditional. The value type can be of any type in the entire Go language, including another map. Let’s look at some examples.

In the second example above, we created a map called people with people’s names for keys and ages for values. We can use some of the built-in methods in Go’s core to retrieve and manipulate these data points. Note that we also used type inference to define this map with the short declaration.

Using Maps in Go

Let’s look at some of these a little closer. With our first example, we can assign a new key-value pair to the map. Our second example, lets us assign the value to a variable. Our third example, let’s use find the length of the map. The fourth example lets us delete any key-value pair that exists in the map. In our fifth example, we test whether or not a key-value pair exists in the map. We use the underscore as a filler variable. This filler gets assigned to the value under the proposed key. The ok variable is a boolean type that produces true or false if that value exists. Note that we use an underscore for this because we don’t need the value in this case.

map-in-go

The last two cases are self-explanatory, with us using a for loop with the range keyword to iterate over the map. The k variable is assigned to each key in the map and the v variable is assigned to each value. This for loop runs until the map runs out of values. Our final example just allows us to define an empty map without using the make keyword.

Maps Inside of Maps

If you want to create a map that looks more like traditional JSON then you can make a map inside of a map. Multidimensional maps allow us to create large indexes that work like databases. Let’s look at a small example.

In this example, we created a map with two string values that were each bound to a map. inside of those maps were three string values bound to three more strings. We used a multitiered for-loop to unpack this data. The value of x in the first loop iterates from the John and Jane keys. The value of y iterates through the inner value strings.  

After we have talked about interfaces and struct types in Go, we will revisit maps to talk about how we can use structs and interfaces as the key type for multidimensional maps. Interfaces and structs really simplify these types of structures. For now, just keep in mind that maps in Go can use comparable types. These are booleans, numerics, strings, pointers, channels, interfaces and structs. 

Conclusion

In this post, we looked at hash-maps in Go. We looked at some of the ways we can make these maps and some of the methods by which we can access and use the data inside of these maps. In our next few posts, we will start to look at typical functions, variadic functions and recursive functions in Go.