This lesson is locked. Login or Subscribe for more access!

Intro to maps

Duration: 5 mins

Learn about maps in Go; how to make 'em and how to use 'em.

Instructor

Matt Boyle

Share with a friend!

Transcript

So in this video, we're going to learn a little bit more about Go maps.

This is going to be very beginner friendly. So if you don't even know what a map is, do not worry. By the end of this video, you will.

So in Go, a map is an unordered collection of key value pairs. Each key is unique within a map and it maps to exactly one value. Maps are highly efficient. They're great for lookups, additions, deletions, which makes them great for things that have unique identifiers, for example, caches.

There are several ways to create a map in Go. The most common method is using the make function, which initializes and allocates memory for the map. So let's see how to do that. If we make a map called people, this is a map where we can store people's names and their age. One example would be a person called John and they're 42, a person called Jane and they're 43, a person called Jack and they're 44. This is really simple. This is how you can create a map and you can start adding people to it.

Now, if I wanted to, I can get back a value for one of these people too. So say we wanted to find out how old John is, all we would need to do is j, ok := people["Jack"]. Then we need to check if it's ok and print j. So let's run our program now and see what the output is, and it's 44.

Now what would happen if we tried to look up someone who didn't exist? Let's put in Matt, who we have not added to our map, and let's get rid of this check for ok. Just put an underscore here to show that I'm not going to use it, and you can see I don't get an error, but I get the zero value back of the type of the value. So one nice thing you can do here is, you saw me do it already, but we can do ok which, when we look up in a map, gives us the value and a boolean as to whether we are actually using that key within the map. If I want to, I can do this: if !ok { fmt.Println("Matt not in map") }, and maybe we end our program there. This gives us a lot of power to do pretty useful things. If I run this again, you'll see “Matt not in map.” This can be really useful because, imagine we’re using this as a cache and discover some key is not in the cache—this gives us the ability to go and look up that user in the database and then add it to our map. So a very, very powerful, very simple data structure to use.

Now, what if we want to update an element in the map? We've added John here, who's 42, and it's just been his 43rd birthday. Well, that's easy too, because all we have to do is look up the same key again: people["John"] = 43. That's it, as simple as that. We've just updated the value of John.

Now let's say we want to remove him. Let's say he worked for us and he's gone to work somewhere else. How do we do that? Very, very easy. All we have to do is delete(people, "John"). And if we then print our map beneath this, you'll see that he's no longer in the map. Again, very, very easy to manage.

The final thing we might want to do with a map is iterate over it. So the same way we can with an array, maybe we want to go through a map and see all of the people in it. We can do that with something like: for name, age := range people { fmt.Println(name, "is", age) }. Very nice to do.

Now, one thing to always be aware of is you cannot depend on the order of a map. This is by design. It's not concurrency safe, and you can't depend on the order. Let's run this a few times to prove that. We added John first to our map, but when we iterate over it, Jack's value is returned first. Be really, really careful: if you care about order, do not use a map. You should use an array or a slice instead.

That's everything you need to know to get started with maps. Hopefully it's given you a new tool in your tool belt, and you can now begin to use maps alongside some of the other data structures I'm sure you already know, like slices.