Welcome back.
In the last lecture, you learnt that you can read from an initialised map, but you cannot write to
it.
For example, you can get the length of a map and you can check whether the key exists inside a map
or not.
However, you cannot add a new key value pair to an initialized map.
To do that, you need to initialize it.
You can do so by using a map literal.
This is a map literal, which creates an empty map.
This part declares that this is a map.
This is the key type of the map and its value type.
This is an empty map because it doesn't contain any key value pairs yet.
So its element list is empty.
However, it's an initialized map.
Remember, composite literals like a map literally always creates initializes and returns a composite
value here.
It creates an initialized and empty map and returns it.
That's why you can assign to this map.
OK, let me get the English words from the command line argument.
Instead, they command this.
Now let's use the query as a key to the map like so.
Cool, it works, as you can see, the map contains the translations of the up and down words using
these assignments, I have added new players to the map instead of an empty map.
Let's create the map with some initial pairs like so.
Here, each key value pair is separated with a column, so these are the keys and these are the values
that you use a map literal like saw behind the scenes.
Go initializes the map with this key value pairs.
As you can see now, the map contains five key value pairs and you can retrieve all of them also.
Since the key top of the map is a shrink, I cannot add a value with another type.
For example, let me try to add in Turkey is you can see it doesn't work.
Likewise, I cannot use a value with another type other than a shrink.
It's because the value type of the map is a string.
Now, that means.
You cannot use different type of values because gold is a stronger type programming language, so all
the keys in the map should belong to the same key type.
In addition to that, all the values in a map should also belong to the same value type.
OK, I had incorrectly translated the first word.
Let me fix it, I can directly change the value inside the map, literal level, but sometimes you cannot
or don't want to access the original map literal.
In those cases, you can change it by directly assigning to the same key LACHSA.
As you can see, the map just overrides the previous value that belongs to the gold key.
So if the key doesn't exist, the map creates a new pair, but if the key exists, it overrides the
pair here.
That's why the map has added the up and down keys as two new pairs because they didn't exist before.
However, the good key was in the map.
So the map has overwritten its value, by the way.
Normally you don't have to declare a variable for a key.
I could also have typed the key directly like I saw here.
These are the keys and these are the values.
Let's take a look at what happens when the user requests and an existing key like so it works.
However, let's say you want the program to print the message when it cannot find the given word.
As you can see, it prints the real value of the map value type.
Let's check for that LACHSA.
Good, it works, let's say I assign the word to the map with zero value by mistake.
What do you think?
What will happen if I ran the program with the mistake word?
Please post the video and try to answer.
All right, let's try it.
Even though the mistake word is in the map, but the program couldn't find it, it's because here I
check whether the returned value from the map is an attempt to shrink or not.
However, in the map, the corresponding value for the mistake word is an empty string.
So this court cannot detect whether the mistake word exists or not.
This may not seem important here, but it can lead to Saddlebacks later on.
So better fix it.
So how can you detect whether a key really exists in a map?
Fortunately, there is a way.
All you need is to add another variable here.
So let me show you the type of this variable.
It's bool variable.
As you can see, when you want to retrieve a value from a map using a single variable, it just returns
a single value.
However, then use one more variable.
It also returns to bool value.
This bool variable tells you whether a key exists in the map or not.
So if the given key doesn't exist, the OK variable becomes false, otherwise it becomes true.
So instead of checking for the string zero value, let's use the OK variable.
As you can see now, the program works correctly, it can detect whether a key exists or not.
By the way, you can also check for a key.
In short, if Lexar.
Of course, now the value is in the scope of the if statement, so I need to exchange the not found
message with the happy path message lachsa limit.
Also remove the not from here.
So when the program finds a word, it will print the words and quit before they're not found.
Message.
As you can see, it still works as before, so they don't want to introduce a new variable within the
current scope.
You can use the short if statement as in here is you can see a map provides us a first look up through
a key.
However, I often see programmers want to loop over a map to print all the keys like.
So as you can see, you can range over a map.
Each loop step assigned to values to two variables.
The first variable stores the key and the second one stores the value.
Let me run it a few times.
As you can see, the order of the Keys, sometimes change is because behind the scenes and map stores,
the keys and values on orders, when you want to loop over a map, be careful.
It might indicate a design problem in your software is because maps have been designed for the fast
lookups, but not for fast traversal.
If what you want is fast looping and a slice, maybe a better candidate.
However, sometimes, especially in testing, you might want to print the map values to compare them
for equality.
To do that, you can print a map like so.
As you can see, the front package always prints the map sorted by key, by the way, if you see an
unordered output, then you need to update your gold version.
This feature has been added beginning with goal one, that 12.
You can also use Sprint to create a screen representation of a map so that you can compare it to another
map.
Let me show you first.
I'm going to get the Gosforth representation of the map from here, then I'm going to use it in a new
map variable.
Let me get the screen representation of the maps lachsa.
Remember, Sprint is just like printf.
The only difference between them is that Sprint returns a string value instead of printing it.
OK, lastly, I'm going to print only if they are equal.
OK, as you can see, the maps are equal.
All right, that's all for now.
I will continue talking about maps in the next lecture.
See you there.