This course has been updated! We now recommend you take the Basics of Go course.

Check out a free preview of the full Go for JavaScript Developers course:
The "Maps" Lesson is part of the full, Go for JavaScript Developers course featured in this preview video. Here's what you'd learn in this lesson:

Brenna introduces maps in Go, a data structure which is made up of key value pairs.

Get Unlimited Access Now

Transcript from the "Maps" Lesson

>> So mapping Go is gonna be similar to what we think of is kind of like an object in JavaScript, an object shape with key value pairs. In this example here on line seven, we're establishing that we're creating a variable called userEmails. And the type is gonna be a map instead of a slicer in array.

[00:00:18] And the type signature now has two different values. The value in the hard brackets indicates what type is the key, and the value on the right is what type is the value. So here we're looking at an object type structure that has keys that are ints and values that are strings.

[00:00:34] And on lines nine and ten, we're setting those values. So on our userEmails map, a key of one will have a value of And a key of two will have a value of Cool. So let's add this into code here and we'll see what happens. And hop over to my maps.go file.

[00:00:59] And grab. Code here. Cool. So similar to the slide. We're setting up ideally an empty space for a map. And then we're assigning a couple variables to it, values to it, excuse me, run the code. And you'll notice that we're getting a panic error, assignment to entry in mil map.

[00:01:59] An error we're seeing here is for the exact same reason we saw the error in our slice example. So because we have a map, Go knows that at some point I'm gonna have a bunch of key value pairs but I have no idea how many you're gonna start this out with.

[00:02:12] So in order to get us going with an actual value, we need to use the make so that Go knows to actually allocate some sort of memory to put values in later. So we can use make. And then what are we making? We're making a map with integers as keys and strings as values.

[00:02:35] Unlike slices, we don't have to tell make how much space to allocate here. And you'll see that it prints out a map type with two key value pairs. An obvious difference you'll notice right off the bat compared to JavaScript is that when this prints out, you're looking at two hard brackets and they're not comma separated.

[00:02:56] But the pattern of having the key value pair is very similar. Similarly to when we were talking about slices, we can do this with a shorthand syntax. We'll start with the shorthand syntax for defining a variable, userEmails, the type is a map in string. And then we'll open it up with curly braces to add our initial values.

[00:03:23] So in here we have a key of one. The value is set to, and then And because I'm not printing it, we'll go ahead and copy line 12. And you'll see that same output with the two elements within our map. Similar to JavaScript, if we wanted to mutate or modify these elements in any way, we can simply say userEmails of key one.

[00:04:14] And I want you to be So if we wanna modify our userEmails in some capacity, we can modify the element with a key of one and change that to be Then when we print userEmails, you will now see the updated value where user1 is now, sorry, the element with a key of one is

[00:04:45] Cool, so a super interesting part about maps in Go is that Go has a pretty unique way of verifying whether or not an element exists in a map. So to do that, we are going to set our map lookup to a couple variables. So what that looks like is let's say we want to check out some sort of first email, we're gonna set that to a variable of userEmails of one.

[00:05:12] If I print this guy. You'll see we have So it's returning the value that I'm getting when I look up key one of userEmails. What happens if I lookup a key that doesn't exist? Let's say we look up like four. So there's no key four in our userEmails map at the moment.

[00:05:40] So notice I get nothing back. So that's helpful in its own, right? But it doesn't blow up. We can still be like, okay, that's a mil so do something about that. But Go also provides a second argument here, which is a Boolean that indicates whether or not something was there in the first place.

[00:05:54] Cuz now we have two different variables. And if I print out what is first email and what is the okay variable, we're gonna see that first email is an empty string. It's on the left of this false that you can't see. But on the right-hand side you see this actual explicit false, which is a kind of cool pattern of like, just tell me if something's not there so I can do something about it.

[00:06:15] And really have a readable way to figure out what's going on here. Cool. So let's kind of combine this syntax where we were checking to see if something exists in a map with the if blocks that we talked about kind of earlier on today. So if we're saying, okay, I'm looking up some email in a user database.

[00:06:39] And I wanna do something with that if it exists, we're gonna use the pattern that we also see in for loops using the range keyword. So on the left-hand side we have two variables, the email and whether or not that's present. And we're setting that to a value of user emails of index for index one, whatever index we're looking at.

[00:06:57] And by index, I mean a key amount, key value. We're looking up userEmails with a key of four, semi colon, and then the word okay. And so here what's happening is that these two variables on the left-hand side of the if are going to be assigned to whatever gets reported back by this map lookup of userEmails key of four.

[00:07:26] Then it's like, all right, so if the okay, this is actually the conditional we're talking about. If okay is true, execute what's in these curly braces. If okay is false, don't, do something else. So here we can say something like. Email exists. Else. Email doesn't exist. And we're back to an error that says email is declared or not used.

[00:08:07] And similarly to when we're working with the for loop, if we don't wanna use the actual email, we can replace that with an underscore. And we're seeing that hey, when you tried to get a key a four from userEmails, okay, return false. So we hop back down to our else block, which prints email does not exist.

[00:08:26] The last topic about maps I'd like to cover real quick is the method, delete. And delete is gonna take two things, it's gonna take what map are we talking about? And what's the key we're looking for? What do we wanna delete? So here I'm saying, hey, in userEmails find a key of two, and if it exists, delete it.

[00:08:48] And we'll finally print out our last version of userEmails. And you'll see that at the end of the day here we've deleted that element with a key of two. And now we're down to just one element in our map.