Welcome back.
In the last lecture, you learned how to initialize and work with a map in this lecture.
You're going to learn how to make long and delete from a map.
At the same time, you'll be learning about the map internals and how they work behind the scenes.
As you know, you have an English the Turkish dictionary program.
Let's say you also want to create a Turkish to English dictionary.
How can you do that?
Well, you need to copy the data from the original map to the new map.
Let's take a look at this in the according Ed.
Let's remove some part of this code first, then we change this word to the correct Turkish word, OK?
The program still accepts an English word from the command line and there are only three word pairs
in the map.
Now, let's add a Turkish dictionary as well.
I'm going to copy the English dictionary to the Turkish dictionary like so.
And I'm going to print both the dictionaries.
As you can see, they both contain the same set of words.
Now I'm going to change a word in the Turkish dictionary and I'm going to change other words in the
English dictionary.
As you can see, all the maps, see the changes that may explain why.
Behind the scenes, this is what a map value looks like.
Let's say it is stored in the fourth memory location.
This is called a map header, like a slice header and map header is also used for tracking everything
about the map.
Here are our map variables.
The first one is the English Dictionary map variable and the second one is a Turkish dictionary map
variable.
As you can see, the map variables do not contain any keys or values on their own.
They only store memory addresses of the actual map header here.
They both point to the same memory location, the fourth.
So when you create a map, go creates a map header behind the scenes, then it returns to memory location
of that map header.
Here, the same memory location is stored in two map variables dict and the Turkish map variables.
So that dict and the Turkish map variables point to the same memory location where the map header is
located in.
That's why when you change one of the map variables, they both see the change.
Let's talk about the differences between a map and a slice value.
My values are like values they do not store and elements on their own, they are pointers to some internal
structures.
The difference between a slice and a map value is that a value is a slice heter.
It stores the slice header directly in itself, in the slice value itself.
As you know, there is a point or field in a slice header that points to a backing array.
So a slice value indirectly points to a backing area.
However, a map value doesn't contain the map.
Hattar itself, unlike a slice value and map value, is only a pointer to a map header.
So a map value itself is a pointer interm.
The map header contains another pointer to the real data, so there are multiple interactions from a
map value to the actual map data.
But the map value itself is not a structure unlike a slice.
So why go creators have designed the slices and maps so differently?
It's because a map header is a complex and a larger data structure than a simple slice header.
Unlike a slice, the map data structure has a lot of moving parts behind the scenes.
So rather than storing it in a value, it is better to store only a pointer to the map header.
So when you assign a map value to a map variable or pass it to a function, you only pass a pointer
to a map header.
In summary, a map value is a pointer to a map header and map header tracks everything about a map value.
So this means that passing maps to a function or assigning them to a variable is very cheap and efficient.
You can pass them around as much as you want, like slice values, map values.
Also, don't carry all the keys and values with themselves.
Of course, this is not the whole story, but it's enough to give you a very good intuition about what
maps really are.
OK, let's get back to the code.
Clearly, you cannot create the Turkish to English dictionary just by assigning the original map value
to a new variable, doing so only copies the pointer off the original map.
You need a separate map value with a new map header.
So instead of copying the map pointer, let's create the new map value using the make function.
So you learn about the make function in the slices section as well.
If you remember it, initializes composite data values.
As you can see, you can also use it to initialize a map because a map is also a composite value.
You pass it the type of the map and the make function in slices, a new map value in the memory and
returns that you might ask the difference between using the make function or creating an empty map using
the map, literally.
So actually there is no difference except a length parameter.
For example, I can give you an initial length to the map like so here I have used the length of the
first map because I'm going to create the same number of keys in the new map.
One way this parameter doesn't work like the slices.
Remember, when you make a slice with a length go creates a slice with that exact length.
However, it is not like so with maps, with maps, the size parameter is just a hint.
So Gore may use it or not.
There is no guarantee.
So don't assume that the magic is going to create a large enough map just because we give it an initial
size.
However, it may help the compiler and runtime on deciding when to grow and where to allocate the map
value.
Sometimes beginners also think that the size parameter is a limit or a capacity for the map, but it's
not.
The map can grow much larger than the given size the size parameter means at least, so it doesn't mean
at most.
For example, let me show you the current length of the map.
As you can see, its length is zero, even though I created the map with a size parameter.
OK, let's Calon the English the Turkish dictionary manually using a loop like so remember keys in the
English dictionary contain English words, but the corresponding values contain the Turkish words or
the Turkish dictionary.
I need to do the opposite so that I can retrieve an English word using a Turkish word.
Let me show you what the English and Turkish dictionaries look like.
As you can see for the new dictionary, I switched the keys and values, now I can get an English word
by using a Turkish word.
Let me comment on this first.
Here, I'm going to check for the Turkish word in the Turkish dictionary, LACHSA.
If there is a match that the print its meaning.
Let's first run it with an English word.
Now let's run it with a Turkish word.
Awesome.
Now you have two separate dictionaries and your program works with both of them.
Let's say you need to add another word to the English dictionary, as you can see, its Turkish translation
is the same as the perfect word about, remember, our learning algorithm uses the values of the original
map as the keys or the new map.
As you learned, a map doesn't accept duplicate keys, so it returns.
Only awesome is because here first it adds as a key and sets its value to perfect.
After that, in the next loop, it overrides the value for the mickum malky with the awesome word.
So there is only one key not to.
Let's say you want to remove the awesome word from the English dictionary before closing.
There is also a built in function that allows you to exactly do that.
Let me show you.
I need to type delete that.
I need to pass the math value.
And lastly, I need to pass the key, so.
As you can see, the delete function has removed the awesome word from the English dictionary, so the
program returns perfect instead of awesome.
What happens if you call the delete function again and again, LACHSA and one more time?
The nonexisting key is you can see nothing happens.
Sometimes I see that people first check whether there is a key or not on the Dan they call the delete
function.
As I just showed you, you don't have to do that.
You can use delete function directly.
By the way, if you want to delete all the keys and values from a map, you can do it like.
So as you can see now, the program cannot find anything because the map is empty.
Why do you think have I correctly destroyed the map here this past video and answer?
Actually, now the real math value is still there in the memory somewhere here by setting this map variable
to nil.
I have only changed the value of this variable.
So this variable no longer holds a pointer to the real map.
So how can you really destroy a map?
To do that?
You need to change over the map like so then you need to call the delete function with a key.
It works as before, but behind the scenes go change this whole loop with a single map clear command.
So don't be mistaken that it loops over all the keys off the map.
This is a magical operation behind the scenes.
Go convert it to a single operation.
It's very efficient.
All right.
That's all for now.
Congrats.
The map section is complete.
Thank you for watching so far.
See in the next section by.