Golang Tutorial #15 - Maps

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone and welcome back to their golang tutorial so in this video we're gonna talk about maps now maps allow us to store what's known as key value pairs so a key maps to some value that's why it's called a map so contrasting this to arrays where we had something that looked like you know all these random values the way we access these was by their position in the array so the order in which they occurred and arrays are really good at storing ordered data where the order of these information actually matters or most of the time it does and in fact to access the values we know that the first value we access with index 0 and the last one we would access with the length of the array minus 1 so the positions really matter and that's how we access elements now in some cases we actually don't care about the positions or the indexes we just care about the presence of elements right so we care about some key mapping to some value it doesn't matter what order they're in and in fact a map does not maintain any order so I'll start showing some examples so this makes sense but to create a map this is what we do something like var MP and then we can define the type that we want this map to be so there's a few different ways and I'll go through them we write map we put what type we want the key to be here in this case I'm gonna put string and then we put what type we want the value to be at the end so what this is saying is I'm making a map that has string keys that point to integer values so an example of a string key pointing to an integer value could be something like apples pointing to one maybe signifying that we have one Apple or we want to buy one Apple maybe it's a grocery list or something right whatever but this is an example of a mapping we have the key Apple or Apple's mapping to the value 1 and that's because this is a string and this is an int so the most common way to make maps I guess could be a map literal or I guess a map instantiation or whatever you want to call it but a map literal just means we actually start defining some key values we're gonna have in the map to start so to do that we define VAR m P we can put the type that we want although we don't need to put that there we're actually gonna copy the same thing again and then we're gonna put our curly braces like this and inside of here we can define our mappings so we define key values and of course we can add more things we can delete things from this we can check if things exist but we'll start with a few values just for this example so I'm gonna say actually yeah Apple let's put that at five and let's put pear at six and then let's put orange inside of quotation marks that would be important at nine okay now it is important that you add a comma after your last element unless you're gonna do a newline that's just something in goaling you might see an error so just add the comma at the end it doesn't hurt to do that okay so I'm gonna save this after I go ahead and print out FMT I'm gonna print out MP and just show you what this looks like okay so let's go go run tutorial go let's have a look at our map down in the console here okay so we get Apple orange pear and it gives us the values now one thing I want you to notice immediately is that these was that elements here mappings are not in the same order in which we inserted them that's because a map does not keep track of order whatsoever it just knows that this key references that value it knows if a key exists but it does not keep track of the order in which we insert things so that's something that's important if you care about the order of data then you're not gonna want to be using a map okay so we have apple orange pear that is the map now let's show a few other things I'll show you how to we can make other ways to make maps so another way to make map is to do like something like MP colon equals and then we could set it equal to that literal that we just did here or we could say MP equals make and inside of here we just put map string int now this will make an empty map for us that we could use I can't call as MP again just cuz this one's called MP but this is another way to make a map one of the most common and this makes an empty map so just keep that in mind these are probably the two ways you're gonna use there's a few other ways but if you need that you can look them off they're not that difficult okay so we have the map I'm gonna show you now how we can change values delete values add values so to change and access values what we do is we put MP and then inside of square brackets we put the name of the keyway on axe so if I wanted to access the value of Apple I would say fmt dot println MP at Apple so I usually call this like act or index but like inside of here you just put the key if I put pear that would get me six if I put orange that would give me nine apple will obviously return to be five so let's print this out and see what our output is to make sure I'm not lying and we get five and then the map so that is how you access the values knowing that you can quite easily change the values so I can do something like MP Apple equals nine hundred so what I've done here is I've set MP at the key Apple equal to nine hundred and coincidentally this is the same way that you add new values so let's just run this and I'll show you the change so Apple starts at five we print it out and then you can see that it changes to nine hundred afterwards so that's how you change values now to add values all you have to do is the same thing except you're just putting a key that doesn't already exist so if I put I don't know like some random I don't want to stick with fruit so I should put Tim say MP Tim equals 900 what's gonna happen now if we run this is we will add a new key value Tim 900 so we get that Tim 900 when that was pretty easy that's how you add a new value so that's how you update change access and add new values now let's see how we can delete them so to delete a values pretty easy you write the delete function you write the name of the map and then you follow that by the key that you want to delete so I'm gonna put Apple inside of here what this will do is it will check if Apple is inside the map if it is there it will delete it if Apple is not inside the map it just won't do anything it won't delete that key so let's run this now and have a look at what we get and we get nice so we get that key deleted we can see that's different than the one from up above all right so that is the basics on maps I'm gonna show you now a few kind of tricks and things that are useful to do with them so one of the things that you typically want to do with maps is you want to check if a key exists now that's because you don't want to go about accessing that value if the key doesn't exist and sometimes you know you need to do something like if the key doesn't exist add it in like stuff like that so to actually check if a key this inside of a map and to possibly get the value you write an expression that looks like this Val comma okay colon equals MP at the key that you want so in this case I'll put Apple now what this says is if the key Apple exists store the value in Val if it does not make Val whatever the default type is for the type of the map so that in this case it would just be 0 because that's the default value for int insect this okay variable to represent whether the key exists or does not so okay will be set to true in the case of Apple because the key app will exist but if I put in here let's say tim-tim does not exist in the map right now so okay would be false and the value would be default 0 so let me show you what I mean FM t dot println val okay so will print those two things out and see what we get when we try to access the key Tim so we see we get 0 false the reason for that is because the default value of int is 0 and false tells us that this key was not inside of the map now if I go ahead and change this to Apple let's run this here and we get 5 true because the value 5 existed and it's true that that is in there so it's giving us true so that is how you can check if a value is inside of a map now one important thing to say about maps this might go a little bit over some people's heads but I feel like it's important that it says that they access values in a very different way on a lower level than then arrays do so in fact accessing values from a map changing values adding values in a map happens and almost instantaneously doesn't matter how big the map is how many keys are inside of it you can kind of assume that it's gonna happen very quickly very fast almost instantly whereas with arrays we've seen how they're represented lower down in the computer and if I wanted to access values in an array that was like 20,000 things long twenty thousand elements long I would actually have to potentially look at all of those elements to grab the element that I want which means typically arrays are going to be slower for certain operations than maps so that just means like only use an array if you actually care about the order in which data is there or you want that kind of structure that array structure with indexes and all of that and same thing with slices so a brazen slices they're pretty well the same in this explanation whereas maps will not store any order whatsoever but they will keep track of a key and a value and it'll make it very fast to access any key and any value so that's the two fundamental differences you'll see if you do any programming problems that maps are used a lot in terms of keeping time complexity pretty simple at what's known as a one-time complex near Big O one I'm trying to think there's anything else I could show you I believe we can get the length of the map by calling the land method let me check that I haven't actually tried this before so let's see if I can do this Len of MP let's see if that tells us how many keys are inside of there so go run tutorial go and we get three so yes we can use the Len of map and that will tell us how many keys are inside of it so I think with that that's all I need to cover for a map I hope you guys enjoyed if you did make sure you leave a like subscribe and I will see you in the next goal and tutorial
Info
Channel: Tech With Tim
Views: 16,431
Rating: undefined out of 5
Keywords: tech with tim, golang, go programming language, golang maps, maps golang, golang maps tutorial, maps go, go maps, go programming tutorial
Id: yJE2RC37BF4
Channel Id: undefined
Length: 9min 56sec (596 seconds)
Published: Fri May 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.