WEBVTT 00:03.170 --> 00:03.980 Welcome back. 00:04.340 --> 00:09.600 In the last lecture, you learned how to initialize and work with a map in this lecture. 00:09.650 --> 00:13.190 You're going to learn how to make long and delete from a map. 00:13.490 --> 00:18.470 At the same time, you'll be learning about the map internals and how they work behind the scenes. 00:18.750 --> 00:21.870 As you know, you have an English the Turkish dictionary program. 00:22.580 --> 00:26.100 Let's say you also want to create a Turkish to English dictionary. 00:26.600 --> 00:27.710 How can you do that? 00:28.100 --> 00:32.630 Well, you need to copy the data from the original map to the new map. 00:33.860 --> 00:35.930 Let's take a look at this in the according Ed. 00:38.860 --> 00:45.550 Let's remove some part of this code first, then we change this word to the correct Turkish word, OK? 00:45.580 --> 00:51.040 The program still accepts an English word from the command line and there are only three word pairs 00:51.040 --> 00:51.550 in the map. 00:52.150 --> 00:54.560 Now, let's add a Turkish dictionary as well. 00:55.210 --> 00:58.890 I'm going to copy the English dictionary to the Turkish dictionary like so. 00:59.930 --> 01:02.060 And I'm going to print both the dictionaries. 01:08.190 --> 01:11.230 As you can see, they both contain the same set of words. 01:12.120 --> 01:17.460 Now I'm going to change a word in the Turkish dictionary and I'm going to change other words in the 01:17.460 --> 01:18.350 English dictionary. 01:20.660 --> 01:24.830 As you can see, all the maps, see the changes that may explain why. 01:26.150 --> 01:29.220 Behind the scenes, this is what a map value looks like. 01:29.720 --> 01:32.570 Let's say it is stored in the fourth memory location. 01:33.080 --> 01:39.980 This is called a map header, like a slice header and map header is also used for tracking everything 01:39.980 --> 01:40.580 about the map. 01:41.210 --> 01:42.800 Here are our map variables. 01:43.070 --> 01:48.710 The first one is the English Dictionary map variable and the second one is a Turkish dictionary map 01:48.710 --> 01:49.190 variable. 01:49.620 --> 01:55.330 As you can see, the map variables do not contain any keys or values on their own. 01:56.120 --> 02:00.530 They only store memory addresses of the actual map header here. 02:00.560 --> 02:04.380 They both point to the same memory location, the fourth. 02:05.030 --> 02:11.810 So when you create a map, go creates a map header behind the scenes, then it returns to memory location 02:11.810 --> 02:12.950 of that map header. 02:13.550 --> 02:20.780 Here, the same memory location is stored in two map variables dict and the Turkish map variables. 02:21.470 --> 02:27.950 So that dict and the Turkish map variables point to the same memory location where the map header is 02:27.950 --> 02:28.800 located in. 02:29.570 --> 02:34.100 That's why when you change one of the map variables, they both see the change. 02:34.700 --> 02:38.810 Let's talk about the differences between a map and a slice value. 02:39.980 --> 02:48.380 My values are like values they do not store and elements on their own, they are pointers to some internal 02:48.380 --> 02:49.150 structures. 02:49.160 --> 02:54.440 The difference between a slice and a map value is that a value is a slice heter. 02:54.890 --> 02:59.810 It stores the slice header directly in itself, in the slice value itself. 03:00.200 --> 03:05.450 As you know, there is a point or field in a slice header that points to a backing array. 03:05.900 --> 03:09.540 So a slice value indirectly points to a backing area. 03:09.860 --> 03:12.890 However, a map value doesn't contain the map. 03:12.890 --> 03:20.540 Hattar itself, unlike a slice value and map value, is only a pointer to a map header. 03:21.290 --> 03:25.100 So a map value itself is a pointer interm. 03:25.340 --> 03:31.910 The map header contains another pointer to the real data, so there are multiple interactions from a 03:31.910 --> 03:34.660 map value to the actual map data. 03:34.880 --> 03:38.830 But the map value itself is not a structure unlike a slice. 03:39.170 --> 03:44.090 So why go creators have designed the slices and maps so differently? 03:44.570 --> 03:50.990 It's because a map header is a complex and a larger data structure than a simple slice header. 03:51.650 --> 03:56.740 Unlike a slice, the map data structure has a lot of moving parts behind the scenes. 03:57.110 --> 04:03.200 So rather than storing it in a value, it is better to store only a pointer to the map header. 04:04.190 --> 04:11.300 So when you assign a map value to a map variable or pass it to a function, you only pass a pointer 04:11.300 --> 04:12.170 to a map header. 04:13.130 --> 04:20.870 In summary, a map value is a pointer to a map header and map header tracks everything about a map value. 04:21.290 --> 04:29.410 So this means that passing maps to a function or assigning them to a variable is very cheap and efficient. 04:29.900 --> 04:34.980 You can pass them around as much as you want, like slice values, map values. 04:35.000 --> 04:38.890 Also, don't carry all the keys and values with themselves. 04:39.920 --> 04:46.730 Of course, this is not the whole story, but it's enough to give you a very good intuition about what 04:46.730 --> 04:47.870 maps really are. 04:48.660 --> 04:50.540 OK, let's get back to the code. 04:51.700 --> 04:57.940 Clearly, you cannot create the Turkish to English dictionary just by assigning the original map value 04:57.940 --> 05:02.810 to a new variable, doing so only copies the pointer off the original map. 05:03.160 --> 05:06.630 You need a separate map value with a new map header. 05:07.150 --> 05:12.970 So instead of copying the map pointer, let's create the new map value using the make function. 05:13.330 --> 05:17.690 So you learn about the make function in the slices section as well. 05:18.340 --> 05:22.030 If you remember it, initializes composite data values. 05:22.390 --> 05:27.770 As you can see, you can also use it to initialize a map because a map is also a composite value. 05:28.420 --> 05:34.810 You pass it the type of the map and the make function in slices, a new map value in the memory and 05:34.810 --> 05:41.200 returns that you might ask the difference between using the make function or creating an empty map using 05:41.200 --> 05:42.060 the map, literally. 05:42.230 --> 05:46.740 So actually there is no difference except a length parameter. 05:47.530 --> 05:53.500 For example, I can give you an initial length to the map like so here I have used the length of the 05:53.500 --> 05:57.370 first map because I'm going to create the same number of keys in the new map. 05:57.810 --> 06:01.000 One way this parameter doesn't work like the slices. 06:01.360 --> 06:07.810 Remember, when you make a slice with a length go creates a slice with that exact length. 06:08.380 --> 06:14.730 However, it is not like so with maps, with maps, the size parameter is just a hint. 06:15.280 --> 06:17.170 So Gore may use it or not. 06:17.410 --> 06:18.510 There is no guarantee. 06:19.060 --> 06:24.520 So don't assume that the magic is going to create a large enough map just because we give it an initial 06:24.520 --> 06:24.880 size. 06:25.300 --> 06:32.290 However, it may help the compiler and runtime on deciding when to grow and where to allocate the map 06:32.290 --> 06:32.690 value. 06:33.220 --> 06:39.580 Sometimes beginners also think that the size parameter is a limit or a capacity for the map, but it's 06:39.580 --> 06:39.910 not. 06:40.360 --> 06:48.310 The map can grow much larger than the given size the size parameter means at least, so it doesn't mean 06:48.520 --> 06:49.300 at most. 06:50.020 --> 06:53.070 For example, let me show you the current length of the map. 06:56.470 --> 07:01.750 As you can see, its length is zero, even though I created the map with a size parameter. 07:02.760 --> 07:10.050 OK, let's Calon the English the Turkish dictionary manually using a loop like so remember keys in the 07:10.050 --> 07:16.410 English dictionary contain English words, but the corresponding values contain the Turkish words or 07:16.410 --> 07:17.540 the Turkish dictionary. 07:17.550 --> 07:23.340 I need to do the opposite so that I can retrieve an English word using a Turkish word. 07:24.000 --> 07:27.300 Let me show you what the English and Turkish dictionaries look like. 07:28.460 --> 07:34.730 As you can see for the new dictionary, I switched the keys and values, now I can get an English word 07:34.730 --> 07:36.120 by using a Turkish word. 07:36.860 --> 07:38.570 Let me comment on this first. 07:41.560 --> 07:45.970 Here, I'm going to check for the Turkish word in the Turkish dictionary, LACHSA. 07:47.800 --> 07:50.440 If there is a match that the print its meaning. 07:52.440 --> 07:54.580 Let's first run it with an English word. 07:54.960 --> 07:56.880 Now let's run it with a Turkish word. 07:57.720 --> 07:58.230 Awesome. 07:58.560 --> 08:03.110 Now you have two separate dictionaries and your program works with both of them. 08:04.090 --> 08:10.600 Let's say you need to add another word to the English dictionary, as you can see, its Turkish translation 08:10.600 --> 08:17.740 is the same as the perfect word about, remember, our learning algorithm uses the values of the original 08:17.740 --> 08:20.290 map as the keys or the new map. 08:20.890 --> 08:25.930 As you learned, a map doesn't accept duplicate keys, so it returns. 08:25.930 --> 08:33.360 Only awesome is because here first it adds as a key and sets its value to perfect. 08:33.940 --> 08:40.480 After that, in the next loop, it overrides the value for the mickum malky with the awesome word. 08:41.140 --> 08:43.690 So there is only one key not to. 08:44.140 --> 08:48.790 Let's say you want to remove the awesome word from the English dictionary before closing. 08:49.150 --> 08:53.170 There is also a built in function that allows you to exactly do that. 08:53.440 --> 08:54.130 Let me show you. 08:54.430 --> 08:56.350 I need to type delete that. 08:56.350 --> 08:58.160 I need to pass the math value. 08:58.360 --> 09:01.210 And lastly, I need to pass the key, so. 09:03.330 --> 09:08.700 As you can see, the delete function has removed the awesome word from the English dictionary, so the 09:08.700 --> 09:11.380 program returns perfect instead of awesome. 09:11.790 --> 09:17.640 What happens if you call the delete function again and again, LACHSA and one more time? 09:17.640 --> 09:20.940 The nonexisting key is you can see nothing happens. 09:21.450 --> 09:27.840 Sometimes I see that people first check whether there is a key or not on the Dan they call the delete 09:27.840 --> 09:28.320 function. 09:28.470 --> 09:31.110 As I just showed you, you don't have to do that. 09:31.170 --> 09:33.150 You can use delete function directly. 09:33.360 --> 09:38.760 By the way, if you want to delete all the keys and values from a map, you can do it like. 09:38.770 --> 09:43.740 So as you can see now, the program cannot find anything because the map is empty. 09:44.100 --> 09:49.710 Why do you think have I correctly destroyed the map here this past video and answer? 09:50.850 --> 09:58.770 Actually, now the real math value is still there in the memory somewhere here by setting this map variable 09:58.770 --> 09:59.280 to nil. 09:59.400 --> 10:02.520 I have only changed the value of this variable. 10:03.120 --> 10:06.710 So this variable no longer holds a pointer to the real map. 10:07.470 --> 10:09.920 So how can you really destroy a map? 10:10.530 --> 10:11.170 To do that? 10:11.190 --> 10:17.160 You need to change over the map like so then you need to call the delete function with a key. 10:18.900 --> 10:25.430 It works as before, but behind the scenes go change this whole loop with a single map clear command. 10:26.040 --> 10:29.460 So don't be mistaken that it loops over all the keys off the map. 10:29.910 --> 10:32.520 This is a magical operation behind the scenes. 10:32.520 --> 10:34.650 Go convert it to a single operation. 10:35.100 --> 10:36.120 It's very efficient. 10:36.990 --> 10:37.620 All right. 10:37.800 --> 10:38.740 That's all for now. 10:38.970 --> 10:39.620 Congrats. 10:39.840 --> 10:41.680 The map section is complete. 10:42.090 --> 10:43.440 Thank you for watching so far. 10:43.560 --> 10:45.230 See in the next section by.