WEBVTT 0 00:01.110 --> 00:02.640 Welcome. In this lecture, 1 00:02.640 --> 00:08.280 I'm going to talk about the basics of slices and without focusing on their unique properties yet. You'll 2 00:08.280 --> 00:12.360 understand the differences between a slice type and an array type. 3 00:12.360 --> 00:13.520 All right! Let's get started. 4 00:14.830 --> 00:18.480 Now you might ask: Why should I care about a slice? 5 00:18.490 --> 00:23.500 OK, let's take a look at the primary differences between an array and a slice. 6 00:23.500 --> 00:26.560 Here is an empty array with two elements. 7 00:26.650 --> 00:29.860 As you know an array is a fixed collection type. 8 00:29.860 --> 00:34.090 So if you try to add a new element to this array it won't work. 9 00:34.090 --> 00:37.120 This is because you cannot change its length. 10 00:37.120 --> 00:39.790 Here is a slice with two elements. 11 00:39.910 --> 00:43.030 It looks like an array but there is a subtle difference. 12 00:43.030 --> 00:50.390 So you can add new elements to grow this slice in runtime like this or you can remove elements from it. 13 00:50.410 --> 00:56.920 So how you can add new elements to a slice in runtime while you cannot do so with an array. Let's take 14 00:56.920 --> 01:04.200 a look at an array variable. So, why you cannot add new elements to an array or remove the existing elements 15 01:04.200 --> 01:05.330 from it? 16 01:05.430 --> 01:11.880 It is because the length of an array is a part of its type, so its length belongs to compile time. 17 01:11.920 --> 01:15.610 This means that its length cannot change in runtime. 18 01:15.610 --> 01:18.630 You know, Go is a statically-typed programming language. 19 01:18.630 --> 01:22.890 So when you declare a type, you cannot change it in runtime afterward. 20 01:22.980 --> 01:25.740 So its length cannot change in runtime (an array's). 21 01:25.740 --> 01:30.150 So that's why an array cannot grow or shrink dynamically in runtime. 22 01:30.180 --> 01:35.910 However, most of the time we need dynamically sized collections and arrays don't pay the bill. 23 01:35.940 --> 01:37.740 We need something more powerful. 24 01:37.800 --> 01:41.080 We need slices. 25 01:41.140 --> 01:43.000 Now let's take a look at a slice. 26 01:43.000 --> 01:45.460 This is a slice variable here. 27 01:45.460 --> 01:48.280 The nums variable's type is an int slice. 28 01:48.280 --> 01:50.740 It looks like an array type, right? 29 01:50.770 --> 01:56.860 However, there is a subtle difference. As you can see, unlike an array a slice doesn't have a fixed length 30 01:56.920 --> 01:57.740 on its type. 31 01:58.120 --> 02:01.420 So a slice's length is not a part of its type. 32 02:01.450 --> 02:07.240 This is precisely the main reason why a slice can grow and shrink in runtime. 33 02:07.300 --> 02:13.900 This means that its length belongs to runtime. So its length can change dynamically in runtime. 34 02:14.290 --> 02:20.390 So you can add new elements to a slice and you can also remove the existing elements from it. 35 02:20.410 --> 02:24.910 Let's take a look at another difference between a slice and an array. 36 02:24.930 --> 02:26.490 Here is an array variable. 37 02:26.490 --> 02:31.010 Remember an array value describes its entire elements. 38 02:31.110 --> 02:31.900 Right? 39 02:31.920 --> 02:39.990 So Go sets all the elements of an uninitialized array to zero-values. Here, nums variable's element type 40 02:40.020 --> 02:49.860 is int. That's why Go sets all its elements to zeros here. As you know, when you call the len function on this 41 02:49.860 --> 02:58.260 array, it always returns 5. It is because an array's length is fixed. It always have the same number of 42 02:58.320 --> 03:01.100 elements. 43 03:01.160 --> 03:06.560 Here is a slice variable. Unlike an array, a slice's zero value is nil. 44 03:07.220 --> 03:15.650 So this means that an uninitialized slice doesn't have any items. Remember, a nil value doesn't mean 45 03:15.650 --> 03:23.300 the absence of a value. It means the value hasn't been initialized yet. Here, the nums variable is nil. 46 03:23.870 --> 03:29.530 So it has a nil value but it still has a type. Its type is an int slice. 47 03:29.930 --> 03:37.630 So, Go knows that this variable is an int slice and it allows you to use the len function on it. 48 03:38.090 --> 03:42.850 So that's why you can still get the length of a slice even when it is nil. 49 03:43.160 --> 03:51.610 So, when you do so, it returns zero because the slice here doesn't have any elements yet. Let's take a look at 50 03:51.610 --> 03:53.170 the last example. 51 03:53.170 --> 03:57.990 Let's get the nums slice's first element. But I cannot do so. 52 03:58.130 --> 04:02.480 It's because it doesn't have any elements yet. 53 04:02.660 --> 04:05.560 Let's try to get its second element. 54 04:05.600 --> 04:07.940 This is also meaningless. 55 04:07.940 --> 04:13.120 Now let's try to get its length again. This time it works. 56 04:13.120 --> 04:17.710 It is because, as I've said, Go knows that this is a slice. 57 04:17.720 --> 04:26.340 Let me show you an array and a slice declaration together one more time. As you can see, a slice doesn't 58 04:26.340 --> 04:32.830 have a length. However an array's length is a part of its type. 59 04:32.830 --> 04:35.910 So a slice can grow and shrink at runtime. 60 04:35.980 --> 04:37.910 However an array cannot do so. 61 04:38.440 --> 04:45.130 That's why most of the time we want to work with slices instead. It's because, as you'll see in this section 62 04:45.430 --> 04:53.230 they are easy to work with compared to arrays, and lastly, the zero value of a slice is nil whereas 63 04:53.230 --> 04:59.690 the zero value of an array is a zero value of all of its elements together. 64 04:59.690 --> 05:06.140 Now let's take a look at the common properties of slices and arrays. A slice or an array can only contain 65 05:06.200 --> 05:08.000 the same type of elements. 66 05:08.060 --> 05:14.460 For example here, the nums slice or the nums array can only contain int elements. 67 05:14.600 --> 05:19.350 As you know from the array section, their element types can be different, of course. 68 05:19.370 --> 05:23.480 For example, they can also store string values or byte values and so on. 69 05:23.990 --> 05:30.920 However, all their elements should have the same type. You can access and change the elements of a slice 70 05:30.980 --> 05:34.840 or an array using the index expressions like so. 71 05:34.880 --> 05:40.640 And lastly, you can get the length of a slice on an array using the len function. As you can see, you 72 05:40.640 --> 05:44.600 can work with slices and arrays almost in the same way. 73 05:44.660 --> 05:50.270 I mean you can use the indexing expressions and the len function on a slice just like you can do so 74 05:50.270 --> 05:51.060 on an array. 75 05:51.110 --> 05:53.340 They have the same interface. 76 05:53.360 --> 05:54.150 All right. 77 05:54.220 --> 05:58.930 That's all for now. In the next lecture, I'm going to show you an example in the coding editor. 78 05:59.000 --> 05:59.990 See you there, bye!