WEBVTT 0 00:01.350 --> 00:02.750 Welcome back! 1 00:02.820 --> 00:08.160 In this lecture I'm going to show you some examples about the differences between slices and arrays 2 00:08.190 --> 00:09.630 in the coding editor. 3 00:09.630 --> 00:10.860 All right let's get started. 4 00:13.080 --> 00:17.120 Let's say you want to list the books and the computer games you have. 5 00:17.190 --> 00:19.650 So let's first start with an empty array. 6 00:19.650 --> 00:25.350 I'm going to declare a books array with five string elements like so. OK let me print it using the v 7 00:25.350 --> 00:25.980 verb. 8 00:25.980 --> 00:26.490 All right. 9 00:26.510 --> 00:35.430 Let me run it. As you can see its length is five and its type is five string array and it contains five empty 10 00:35.430 --> 00:40.870 string values. All right. Let's add some books into this array. 11 00:40.870 --> 00:42.190 I'm going to add Dracula 12 00:44.400 --> 00:47.450 1984 and lastly Island. 13 00:48.330 --> 00:48.880 Okay. 14 00:48.980 --> 00:53.250 Let me run it. As you can see it's length is still five. 15 00:53.270 --> 00:56.000 So it still contains five elements. 16 00:56.000 --> 01:03.130 Even though I've set only three elements of it. OK, now I'm going to add my favorite computer games 17 01:03.130 --> 01:04.370 into a slice. 18 01:04.720 --> 01:08.140 As you can see I didn't declare it's length. 19 01:08.140 --> 01:10.700 This is exactly what makes it a slice. 20 01:10.780 --> 01:12.370 It's length is dynamic. 21 01:12.370 --> 01:16.540 If I had typed the length here, it would be an array not a slice. 22 01:16.560 --> 01:17.310 Okay. 23 01:17.410 --> 01:18.100 All right. 24 01:18.100 --> 01:22.430 Now I'm going to print its type and its elements using the v verb again. 25 01:22.450 --> 01:22.900 All right. 26 01:22.920 --> 01:23.460 Let me run it. 27 01:24.040 --> 01:29.820 I didn't initialize this slice yet so it's zero value is nil. 28 01:29.840 --> 01:34.460 Let me only print its type so you can better understand it OK. 29 01:34.490 --> 01:41.880 Let me run it. As you can see, its type is a string slice but unlike an array it doesn't have a length 30 01:42.060 --> 01:50.630 on its type. Let me show you that I can also use the len function on this slice. Let me run it. As you 31 01:50.630 --> 01:55.180 can see, I can use the len function even though the slice is nil. By the way, 32 01:55.220 --> 01:59.230 I can also check whether this slice equals to nil or not. 33 01:59.270 --> 02:07.200 Let me show. Now it says that the game slice is a nil slice because it hasn't been initialized yet. 34 02:08.420 --> 02:08.780 All right. 35 02:08.940 --> 02:11.300 Let's assign some games to the slice. 36 02:11.360 --> 02:20.810 First I'm going to assign kokémon then I'm gonna assign sims. But it fails. As I told you the slice hasn't 37 02:20.810 --> 02:24.520 been initialized yet so it doesn't contain any elements. 38 02:24.680 --> 02:27.380 So I cannot assign any values yet. 39 02:27.380 --> 02:32.260 OK, let me remove these assignments. Instead I'm going to use a slice literal this time. 40 02:32.420 --> 02:35.570 Let me change this to a short declaration first. 41 02:35.960 --> 02:41.740 Okay now I'm going to add two elements: kokémon and sims okay. 42 02:41.760 --> 02:45.290 As you can see this is just like an array literal, right? 43 02:45.300 --> 02:51.570 The only difference is that it doesn't have a length on its type so it's a slice literal. 44 02:51.570 --> 02:57.340 If I had typed a number here, like this, this would be an array literal instead. 45 02:57.420 --> 03:03.750 As I've said almost everything that you've learned about the arrays in the last section is also available 46 03:03.930 --> 03:04.680 to slices. 47 03:04.800 --> 03:07.900 So I'm not going to repeat them here again all right. 48 03:08.130 --> 03:13.490 Let me run it. As you can see, this time the game slice is not nil. 49 03:13.530 --> 03:16.810 That's why it prints false here. 50 03:17.010 --> 03:21.000 Its length is 2 and it contains two elements. 51 03:21.000 --> 03:25.260 By the way, note that, its type is still a string slice. 52 03:25.260 --> 03:31.770 So it doesn't matter whether it contains any elements or not, its type never ever changes. 53 03:31.770 --> 03:38.030 Unlike an array, a slice this length is entirely unrelated of its type. 54 03:38.030 --> 03:43.470 Wait! I've got new books! So let me declare another array with those books. 55 03:47.090 --> 03:54.360 Let me compare whether the books array equals to the new books array like so. But I cannot do this, 56 03:54.480 --> 03:55.400 right? 57 03:55.470 --> 04:01.890 It's because, the types of these arrays are different. It's because their lengths are different. 58 04:02.010 --> 04:06.060 In addition to this I cannot assign them to each other. 59 04:06.060 --> 04:11.040 This is because of the same reason: Their types are different. 60 04:11.040 --> 04:15.620 Okay let's change the type of this array to five elements like this. 61 04:16.890 --> 04:18.760 As you can see now it works. 62 04:18.840 --> 04:21.510 It is because they have the same types now. 63 04:21.510 --> 04:23.700 Their lengths are the same now. 64 04:24.140 --> 04:33.370 Let me also print the new books array. Okay. Let me rerun it. As you can see the books array has changed. 65 04:34.060 --> 04:38.350 Now there are two arrays with the same elements, right? 66 04:38.350 --> 04:43.680 Remember, an array value contains the entire elements in a single value. 67 04:44.110 --> 04:46.600 But this is not the case with slices. 68 04:46.610 --> 04:53.230 OK, now let's try the same thing using the slices this time. I'm going to declare a new slice variable 69 04:53.380 --> 04:54.700 like this. 70 04:54.790 --> 05:00.260 I'm going to add some elements to the slice pacman, doom and pong. 71 05:00.910 --> 05:05.240 Okay let me also print the newGames slice here like this. 72 05:05.270 --> 05:13.770 Okay let's first try to compare our previous games slice to this newGames slice like this. As you can 73 05:13.770 --> 05:15.870 see I cannot do this. 74 05:15.870 --> 05:23.220 This is not because the slices have different lengths. It is because a slice value can only be comparable 75 05:23.310 --> 05:24.680 to a nil value. 76 05:24.690 --> 05:25.530 No worries though, 77 05:25.560 --> 05:28.910 I'll explain why this is like so in the next lectures. 78 05:28.950 --> 05:34.110 So how can you compare whether a slice is equal to another slice? 79 05:34.110 --> 05:38.310 One of the idiomatic ways is comparing them using a loop. 80 05:38.430 --> 05:41.940 I'm going to fast forward this because you already know all about this. 81 05:42.000 --> 05:50.260 Okay here I've removed the if statement first and I've declared a new string variable as you can see 82 05:50.380 --> 05:54.050 you can range over a slice just like an array. Here, 83 05:54.070 --> 06:00.880 I'm checking whether the slices contain the same elements at the same index positions or not. 84 06:01.100 --> 06:04.480 Okay. Let me run it. As you can see they are not equal. 85 06:04.690 --> 06:12.760 The games slice contains kokemon and sims; whereas the newGames slice contains pacman, doom and pong. 86 06:12.760 --> 06:18.940 The interesting thing here is that unlike an array a slice doesn't have a length on this type. So I can 87 06:18.940 --> 06:22.810 assign slices with different lengths between each other. 88 06:22.810 --> 06:23.350 Let me show you. 89 06:23.620 --> 06:27.880 Now I'm going to assign the game slice to the newGames slice like this. 90 06:28.150 --> 06:31.320 As you can see there isn't any error, right? 91 06:31.390 --> 06:37.480 Let me run it as you can see now they are equal. It is because now of the newGames and the games are 92 06:37.480 --> 06:38.180 the same. 93 06:38.320 --> 06:39.770 Look at their elements. 94 06:39.820 --> 06:40.920 They are the same. 95 06:41.020 --> 06:44.580 So it doesn't matter whether their lengths are the same or not. 96 06:44.740 --> 06:50.980 I can do so because the lengths of a slice isn't a part of its type. Only its element type is a part 97 06:50.980 --> 06:55.240 of its type. As you can see here, those slices have the same type. 98 06:55.240 --> 06:57.150 That is why I can assign them. 99 06:57.280 --> 06:59.440 So their length isn't important. 100 06:59.440 --> 07:01.930 Of course there are many hidden subtleties here. 101 07:01.930 --> 07:07.120 Assigning a slice to another slice does some interesting things behind the scenes. But no worries, I'll 102 07:07.120 --> 07:09.070 explain how it works and what it does 103 07:09.070 --> 07:12.430 in this section as well. There are a lot of things to explain here. 104 07:12.670 --> 07:17.980 But it's also good because there are many things to learn! By the way I can also set a slice to a nil value 105 07:17.980 --> 07:19.420 like this. 106 07:19.690 --> 07:24.160 When I do so, this slice will lose track of all its elements. 107 07:24.220 --> 07:30.100 Let me run it. Wow! It says that the slices are equal but they shouldn't be equal. 108 07:30.110 --> 07:32.320 Oh my God! What's going on here?! 109 07:32.330 --> 07:35.610 Games slice is nil but the newGames slice is not. 110 07:35.810 --> 07:37.190 So what is the problem here? 111 07:37.190 --> 07:50.090 Can you find it? Please pause the video and think about it. 112 07:50.430 --> 07:50.750 OK. 113 07:50.760 --> 07:53.480 The problem is this evil range clause. 114 07:53.520 --> 07:55.030 Let me explain. 115 07:55.080 --> 07:57.400 Go detects that the slice is nil. 116 07:57.540 --> 08:00.580 And it doesn't even care to run the range clause here. 117 08:00.600 --> 08:03.260 So this is completely a nil operation. Actually, 118 08:03.350 --> 08:08.340 this is a very good thing, it's because you can use a slice in a range clause even if it's nil. 119 08:08.340 --> 08:11.270 So your code won't crash even in that case. 120 08:11.280 --> 08:16.460 Remember! "Go loves you"! Well, to solve this problem, 121 08:16.620 --> 08:20.700 I can simply check whether the game's slice is nil or not like this. 122 08:20.700 --> 08:21.600 Right? 123 08:21.630 --> 08:26.720 So if it's nil, I'm going to set the ok variable to "not " as I do so in the loop above. 124 08:27.120 --> 08:27.440 OK. 125 08:27.450 --> 08:28.430 Let's try it. 126 08:28.470 --> 08:28.860 Good. 127 08:28.860 --> 08:31.210 Now it says that the slices are not equal. 128 08:31.230 --> 08:32.480 It works. Awesome! 129 08:32.520 --> 08:39.090 But what would happen if I set the games slice to a slice literal without any elements like this. Can 130 08:39.090 --> 08:40.870 you guess what the program will print? 131 08:40.980 --> 08:47.640 Please pause the video now and try to answer. 132 08:47.750 --> 08:48.920 OK let's see. 133 08:48.970 --> 08:50.820 It says that the slices are equal. 134 08:50.830 --> 08:55.000 But why? This is because the games slice is not 135 08:55.000 --> 08:56.950 nil now, it's empty. 136 08:56.950 --> 09:04.060 However, there is a subtle difference between a nil slice and an empty slice. An empty slice is an initialized 137 09:04.060 --> 09:05.080 slice. 138 09:05.140 --> 09:06.070 So it's not nil. 139 09:06.820 --> 09:10.650 However, a nil slice is an uninitialized slice. 140 09:10.690 --> 09:12.070 So it is nil. 141 09:12.070 --> 09:15.140 Remember, nil means uninitialized. 142 09:15.160 --> 09:22.690 OK, so, why should I do? OK, here, instead of comparing the games slice to a nil value, I should compare their 143 09:22.690 --> 09:23.970 lengths instead (the slices'). 144 09:24.550 --> 09:30.850 So I'm going to compare whether the length of the game slice is not equal to the length of the newGames 145 09:30.850 --> 09:33.130 slice or not like this. 146 09:33.130 --> 09:33.570 OK. 147 09:33.580 --> 09:36.790 Let me run it. As you can see now it works. 148 09:36.790 --> 09:38.980 It says that the slices are not equal. 149 09:38.980 --> 09:45.140 So in summary, never check whether a slice is empty or not by comparing it to a nil value. 150 09:45.340 --> 09:53.140 Instead of that check its length. It's because no matter whether a slice is nil or empty, its length 151 09:53.200 --> 09:55.090 will always be zero. 152 09:55.090 --> 09:59.910 So you can hit two devils with one stone in a safer way. 153 09:59.950 --> 10:00.650 All right. 154 10:00.670 --> 10:01.700 That's all for now. 155 10:01.720 --> 10:07.090 This was just a glimpse of the slices and it doesn't show you their real powers. 156 10:07.090 --> 10:11.840 But no worries in the next lectures we'll go deeper into slices. 157 10:11.860 --> 10:13.900 This was just the beginning. 158 10:13.900 --> 10:15.160 See you in the next lecture, bye!