
Lesson Description
The "Maps" Lesson is part of the full, Complete Go for Professional Developers course featured in this preview video. Here's what you'd learn in this lesson:
Melkey introduces maps that are similar to hashes in other programming languages. Maps are defined with key-value pairs and syntax shortcuts for checking the existence of keys within the data structure.
Transcript from the "Maps" Lesson
[00:00:00]
>> melkey: Maps, they are a hash. If you come from any kind of programming language, it's a hash map, right? Key value store, they're called maps in Go. The syntax is actually, I would kind of describe it as maybe wonky. The first time you see it, but let's say we have something called Capital City, and we wanna make a map out of it.
[00:00:19]
So we would use the keyword map, all right? And the first argument in this kind of square notation is the type of the key, right? Cuz again, a map is a key value store. So map, our key is gonna be string types, and the values, let's say, are also gonna be string types, okay?
[00:00:37]
And if you're wondering, you can mix these types. Your key could be string int, bool, whatever, and your value could be any other type. They do not have to be the same, okay? So if we have something like this, we can do USA, oops. All right, so the key is gonna be USA separated with a colon, and then we have the actual value, you can say, "Washington.D.C", okay?
[00:01:03]
We have to append with a comma at the end, we can say "India", we can say "New Delhi".
>> melkey: Okay, and then we can say one more as, like "London".
>> melkey: Or sorry, the UK. London is the capital. Okay, so this is just quick lookups, and then we can do capitalCities and we can use on the keys.
[00:01:28]
You can say "USA", we can print this. Map expression string is not used.
>> melkey: There you go. It was yelling at me. That was really weird error message, but it yelling that we weren't using the return variable here. So capitalCities("USA"). If you run this, we'll get Washington DC.
[00:01:56]
That scared me for a second. I was like, what is that? So yeah, quick, fast lookup. If you wanna check if a key exists before accessing it, cuz let's say we do something like capitalCities, and let's say we don't have Germany, right? We get the, oops, I didn't save it.
[00:02:21]
>> melkey: We get the zero value of a string, right? Because the string is the type for our values here. But if you wanna check if something exists, you can do, I forget the actual term, but we can do is capital, and then the second variable exists. And this is always going to be Boolean, when we deal with maps, right?
[00:02:42]
So it could be true, it could be whatever you wanna call it, but the value type is always gonna be Boolean. And here we could just say, capitalCities, and I can again use our Germany example, all right? And here we can say, if exists, so this is the check, we can add fmt.Println(), and we can say, this is the capital.
[00:03:04]
You put capital, like so, else format println does not exist. Okay, you can save this, we can run and see. Now we now have, does not exist. Cool, okay, kinda gloss over that. There is kind of one more thing you can do, a pre-built function. You can use the delete to specifically delete a key from a map.
[00:03:27]
So we can do delete, and then the map itself, so capitalCities, and then the key. So we can delete UK.
>> melkey: And can do format Printf.
>> melkey: This is new deleted map.
>> melkey: Okay, modular v input C.
>> melkey: So now we have just India and Washington.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops