WEBVTT 00:00.390 --> 00:01.170 Welcome back. 00:01.530 --> 00:07.650 In the last lecture, you learnt that you can read from an initialised map, but you cannot write to 00:07.650 --> 00:07.890 it. 00:07.920 --> 00:14.160 For example, you can get the length of a map and you can check whether the key exists inside a map 00:14.160 --> 00:14.640 or not. 00:15.010 --> 00:19.350 However, you cannot add a new key value pair to an initialized map. 00:19.980 --> 00:22.190 To do that, you need to initialize it. 00:22.530 --> 00:24.700 You can do so by using a map literal. 00:25.470 --> 00:28.150 This is a map literal, which creates an empty map. 00:28.740 --> 00:31.240 This part declares that this is a map. 00:31.740 --> 00:34.770 This is the key type of the map and its value type. 00:35.070 --> 00:39.360 This is an empty map because it doesn't contain any key value pairs yet. 00:39.510 --> 00:41.580 So its element list is empty. 00:42.120 --> 00:44.300 However, it's an initialized map. 00:44.790 --> 00:51.600 Remember, composite literals like a map literally always creates initializes and returns a composite 00:51.600 --> 00:52.980 value here. 00:52.980 --> 00:56.240 It creates an initialized and empty map and returns it. 00:56.760 --> 00:58.980 That's why you can assign to this map. 00:59.550 --> 01:03.180 OK, let me get the English words from the command line argument. 01:03.180 --> 01:05.250 Instead, they command this. 01:06.100 --> 01:09.240 Now let's use the query as a key to the map like so. 01:11.310 --> 01:18.360 Cool, it works, as you can see, the map contains the translations of the up and down words using 01:18.360 --> 01:23.180 these assignments, I have added new players to the map instead of an empty map. 01:23.190 --> 01:26.750 Let's create the map with some initial pairs like so. 01:28.370 --> 01:35.450 Here, each key value pair is separated with a column, so these are the keys and these are the values 01:35.840 --> 01:39.440 that you use a map literal like saw behind the scenes. 01:39.440 --> 01:42.920 Go initializes the map with this key value pairs. 01:43.610 --> 01:49.490 As you can see now, the map contains five key value pairs and you can retrieve all of them also. 01:50.440 --> 01:55.380 Since the key top of the map is a shrink, I cannot add a value with another type. 01:56.020 --> 02:00.790 For example, let me try to add in Turkey is you can see it doesn't work. 02:01.300 --> 02:05.560 Likewise, I cannot use a value with another type other than a shrink. 02:06.100 --> 02:08.990 It's because the value type of the map is a string. 02:09.100 --> 02:09.820 Now, that means. 02:12.150 --> 02:17.700 You cannot use different type of values because gold is a stronger type programming language, so all 02:17.700 --> 02:20.930 the keys in the map should belong to the same key type. 02:21.180 --> 02:26.190 In addition to that, all the values in a map should also belong to the same value type. 02:27.810 --> 02:30.790 OK, I had incorrectly translated the first word. 02:30.930 --> 02:37.860 Let me fix it, I can directly change the value inside the map, literal level, but sometimes you cannot 02:37.860 --> 02:40.560 or don't want to access the original map literal. 02:40.740 --> 02:45.570 In those cases, you can change it by directly assigning to the same key LACHSA. 02:46.460 --> 02:51.250 As you can see, the map just overrides the previous value that belongs to the gold key. 02:51.950 --> 02:58.640 So if the key doesn't exist, the map creates a new pair, but if the key exists, it overrides the 02:58.640 --> 03:00.270 pair here. 03:00.320 --> 03:07.370 That's why the map has added the up and down keys as two new pairs because they didn't exist before. 03:07.730 --> 03:10.150 However, the good key was in the map. 03:10.430 --> 03:13.800 So the map has overwritten its value, by the way. 03:13.820 --> 03:16.940 Normally you don't have to declare a variable for a key. 03:17.480 --> 03:21.100 I could also have typed the key directly like I saw here. 03:21.140 --> 03:24.020 These are the keys and these are the values. 03:27.630 --> 03:34.710 Let's take a look at what happens when the user requests and an existing key like so it works. 03:34.740 --> 03:40.770 However, let's say you want the program to print the message when it cannot find the given word. 03:41.350 --> 03:45.480 As you can see, it prints the real value of the map value type. 03:45.930 --> 03:47.550 Let's check for that LACHSA. 03:51.730 --> 03:58.420 Good, it works, let's say I assign the word to the map with zero value by mistake. 03:58.990 --> 03:59.680 What do you think? 03:59.890 --> 04:03.400 What will happen if I ran the program with the mistake word? 04:04.000 --> 04:06.100 Please post the video and try to answer. 04:09.330 --> 04:11.220 All right, let's try it. 04:13.300 --> 04:19.930 Even though the mistake word is in the map, but the program couldn't find it, it's because here I 04:19.930 --> 04:24.560 check whether the returned value from the map is an attempt to shrink or not. 04:25.180 --> 04:30.280 However, in the map, the corresponding value for the mistake word is an empty string. 04:30.820 --> 04:35.660 So this court cannot detect whether the mistake word exists or not. 04:36.160 --> 04:40.580 This may not seem important here, but it can lead to Saddlebacks later on. 04:40.690 --> 04:41.950 So better fix it. 04:42.220 --> 04:45.940 So how can you detect whether a key really exists in a map? 04:46.780 --> 04:48.290 Fortunately, there is a way. 04:48.520 --> 04:51.580 All you need is to add another variable here. 04:52.000 --> 04:55.240 So let me show you the type of this variable. 04:55.720 --> 04:57.040 It's bool variable. 04:57.430 --> 05:03.250 As you can see, when you want to retrieve a value from a map using a single variable, it just returns 05:03.250 --> 05:04.210 a single value. 05:04.750 --> 05:07.330 However, then use one more variable. 05:07.360 --> 05:09.310 It also returns to bool value. 05:09.880 --> 05:14.540 This bool variable tells you whether a key exists in the map or not. 05:15.070 --> 05:21.580 So if the given key doesn't exist, the OK variable becomes false, otherwise it becomes true. 05:22.120 --> 05:26.680 So instead of checking for the string zero value, let's use the OK variable. 05:29.350 --> 05:35.500 As you can see now, the program works correctly, it can detect whether a key exists or not. 05:36.280 --> 05:38.320 By the way, you can also check for a key. 05:38.320 --> 05:39.790 In short, if Lexar. 05:40.850 --> 05:46.640 Of course, now the value is in the scope of the if statement, so I need to exchange the not found 05:46.640 --> 05:50.570 message with the happy path message lachsa limit. 05:50.570 --> 05:52.550 Also remove the not from here. 05:52.820 --> 05:58.130 So when the program finds a word, it will print the words and quit before they're not found. 05:58.130 --> 05:58.580 Message. 05:59.380 --> 06:05.780 As you can see, it still works as before, so they don't want to introduce a new variable within the 06:05.780 --> 06:06.650 current scope. 06:07.100 --> 06:13.070 You can use the short if statement as in here is you can see a map provides us a first look up through 06:13.070 --> 06:13.440 a key. 06:14.300 --> 06:19.710 However, I often see programmers want to loop over a map to print all the keys like. 06:19.730 --> 06:22.370 So as you can see, you can range over a map. 06:22.820 --> 06:26.050 Each loop step assigned to values to two variables. 06:26.540 --> 06:30.670 The first variable stores the key and the second one stores the value. 06:31.490 --> 06:32.930 Let me run it a few times. 06:36.080 --> 06:42.170 As you can see, the order of the Keys, sometimes change is because behind the scenes and map stores, 06:42.170 --> 06:47.330 the keys and values on orders, when you want to loop over a map, be careful. 06:47.510 --> 06:52.940 It might indicate a design problem in your software is because maps have been designed for the fast 06:52.940 --> 06:55.490 lookups, but not for fast traversal. 06:56.000 --> 07:00.860 If what you want is fast looping and a slice, maybe a better candidate. 07:01.910 --> 07:07.520 However, sometimes, especially in testing, you might want to print the map values to compare them 07:07.520 --> 07:08.520 for equality. 07:09.080 --> 07:11.780 To do that, you can print a map like so. 07:14.670 --> 07:20.730 As you can see, the front package always prints the map sorted by key, by the way, if you see an 07:20.730 --> 07:24.360 unordered output, then you need to update your gold version. 07:24.810 --> 07:29.040 This feature has been added beginning with goal one, that 12. 07:29.340 --> 07:35.610 You can also use Sprint to create a screen representation of a map so that you can compare it to another 07:35.610 --> 07:35.880 map. 07:36.090 --> 07:37.560 Let me show you first. 07:37.560 --> 07:42.930 I'm going to get the Gosforth representation of the map from here, then I'm going to use it in a new 07:42.930 --> 07:43.710 map variable. 07:44.430 --> 07:47.420 Let me get the screen representation of the maps lachsa. 07:48.240 --> 07:50.810 Remember, Sprint is just like printf. 07:51.030 --> 07:56.640 The only difference between them is that Sprint returns a string value instead of printing it. 07:57.470 --> 08:01.520 OK, lastly, I'm going to print only if they are equal. 08:04.000 --> 08:06.190 OK, as you can see, the maps are equal. 08:06.610 --> 08:08.310 All right, that's all for now. 08:08.560 --> 08:11.620 I will continue talking about maps in the next lecture. 08:11.890 --> 08:12.550 See you there.