DEV Community

Cover image for Go from the beginning - using maps
Chris Noring for Microsoft Azure

Posted on

Go from the beginning - using maps

Go from the beginning - using maps

You will have scenarios when you code that there are things you need to look up. If you use a dictionary for example you might look up how you can translate a word from English to Spanish or vice versa. In programming, you have similar situations, maybe you want to know what service is run on a certain port for example. There's also databases that's based around the concept of having a unique key that points to a certain value.

How all this is implemented is via map structure. The idea is that you define a key and value and collect all those in a group, a map.

Creating a map

To create a map in Go, we need to use the following syntax:

map[<key type>]<value type>{ ... entries }
Enter fullscreen mode Exit fullscreen mode

Here's an example of creating a map structure that could hold a phone book:

phonebook := map[int]string{ 555123: "Robin Hood", 555404: "Sheriff of Nottingham"}
Enter fullscreen mode Exit fullscreen mode

We define a map structure with key type int and value type string. Then we assign it value with {}. Each entry is defined according <key>: <value> and separated by comma. So how do we read a value?

Create a map with make()

Another way to create a map us by using the make() function. make() returns a initialized map if you give it a type like so:

dictionaryEnSv = make(map[string]string)
Enter fullscreen mode Exit fullscreen mode

Adding entries

To add entries to the map, you need to provide it with a key and value entry like so:

dictionaryEnSv["hello"] = "hej"
Enter fullscreen mode Exit fullscreen mode

Read a value by key

Imagine now that we have these two entries, and you want the value given that you have then entry 555404, how would we do that? We use the square brackets like so []:

phonebook[555404] // "Sheriff of Nottingham"
Enter fullscreen mode Exit fullscreen mode

Check for existing entry

So you learned that phonebook[555404] gives you a value back. What if it doesn't exist? What happens if you give it a key that's not stored in the map is that you get nothing back:

phonebook[888] //
Enter fullscreen mode Exit fullscreen mode

There's a better way to check this, because accessing an entry with a key actually returns two values, the value, and a boolean. The boolean indicates if this key exists in the map. See this code:

_, exist phonebook[888]
fmt.Println(exist) // false
Enter fullscreen mode Exit fullscreen mode

You can even use this in an if statement:

if _, exist := phonebook[888] {
  // number exist, call person
}
Enter fullscreen mode Exit fullscreen mode

Iterate over a map

We can iterate over a map with a for construct and a range. Here's how you can iterate:

for key, value := range phonebook {
  fmt.Println(key, value)
}
Enter fullscreen mode Exit fullscreen mode

Delete an entry

To remove an entry from a map, you can use the delete() method. The delete() method takes the map and the key to delete as parameters, like so:

delete(phonebook, 555404)
Enter fullscreen mode Exit fullscreen mode

Summary

We've learned what a map is and when to use it. Additionally, we've also learned how to create it, add values to it and read from it, both key by key and iterating over the whole map.

Top comments (0)