1 00:00:00,300 --> 00:00:04,900 One of the fundamental types that you love used frequently in. Go is a slice, 2 00:00:05,400 --> 00:00:09,500 which is a little, like, or an array, but it isn't an array. In 3 00:00:09,500 --> 00:00:13,600 fact, what a slice consists of is a pointer into some 4 00:00:13,600 --> 00:00:17,900 underlying array into some area of memory and a length. A number of elements 5 00:00:17,900 --> 00:00:21,800 that are in that slice that are available and also a 6 00:00:21,800 --> 00:00:25,900 capacity which is the maximum amount of space that could be used so you can add to 7 00:00:25,900 --> 00:00:29,800 the slice and I just want to review briefly this because sometimes the 8 00:00:30,000 --> 00:00:34,800 's causes confusion, it's worth looking at a little bit more carefully. One note, though, when you 9 00:00:34,800 --> 00:00:38,400 pass a slice between functions in go, you're passing a 10 00:00:38,400 --> 00:00:42,900 reference to it. You're not getting a copy and so you would never find 11 00:00:42,900 --> 00:00:46,800 yourself passing a pointer to a slice. It doesn't make sense. Similarly, there are other 12 00:00:46,800 --> 00:00:50,800 types and go which are past like that maps and channels. For example 13 00:00:50,800 --> 00:00:54,900 are passed around as essentially pointers that you don't see. 14 00:00:55,200 --> 00:00:59,600 So you would never find yourself doing star to a map or if you did it would be a very, very unusual. 15 00:00:59,900 --> 00:01:03,800 Raishin. So I've got a simple little program here which just creates slice and then 16 00:01:03,800 --> 00:01:07,900 re slices it and do some manipulation that can help illustrate, what's happening. 17 00:01:08,100 --> 00:01:12,500 So I created a slice here with some numbers in it, instead of six, 18 00:01:12,500 --> 00:01:16,900 six integers and then I re sliced it taking two 19 00:01:16,900 --> 00:01:20,700 elements from it. Now, remember, the slice syntax is 20 00:01:20,700 --> 00:01:24,900 if you're a mathematician, you would think of this as one come to 3. So it's from 21 00:01:24,900 --> 00:01:28,800 one up to but not including the three. So you're going to get two 22 00:01:28,800 --> 00:01:29,700 elements in there. 23 00:01:29,900 --> 00:01:33,100 And so that will give us the first this element 24 00:01:33,100 --> 00:01:37,700 and this element. And what I've done is then I've just modified 25 00:01:37,700 --> 00:01:41,900 something in the underlying slice this this element 26 00:01:41,900 --> 00:01:45,600 here this 11 up, doubled it and then I've added 27 00:01:45,600 --> 00:01:49,900 an appended to the second slice, another number and I'm going to 28 00:01:49,900 --> 00:01:53,700 print out and examine what's happened to the underlying slices. 29 00:01:53,700 --> 00:01:57,700 So let's run it. So the first thing that 30 00:01:57,700 --> 00:01:59,600 comes out 11 and 21. 31 00:02:00,200 --> 00:02:04,700 Is this, so that's these two elements here, which are now in this particular 32 00:02:04,700 --> 00:02:08,900 slice here, this number was then doubled this 33 00:02:08,900 --> 00:02:12,800 one in the underlying one. But when I printed out middle, it's now been doubled there 34 00:02:12,800 --> 00:02:16,700 as well. So just gives you a reminder that you are actually referring to some other 35 00:02:16,700 --> 00:02:20,800 underlying array. This thing didn't make a copy of anything, I 36 00:02:20,800 --> 00:02:24,700 then added something on to Middle. Now, in this 37 00:02:24,700 --> 00:02:28,900 case, you'll notice that middle got 42 added on to the end of it, so there was middle and 38 00:02:28,900 --> 00:02:29,800 it got 42 out. 39 00:02:29,900 --> 00:02:33,800 Don. But this actually affected the underlying slice. When I 40 00:02:33,800 --> 00:02:37,700 print out numbs here, the underlying one you'll notice that 42 41 00:02:38,100 --> 00:02:42,500 has substituted for this here because this append happened on the 42 00:02:42,500 --> 00:02:46,900 underlying slice itself as well as the slides because these are just pointers 43 00:02:46,900 --> 00:02:50,600 into something underneath. Now, One Thing Worth looking at is what the 44 00:02:50,600 --> 00:02:54,100 capacity of the slices of just go in here and say, 45 00:02:56,600 --> 00:03:00,900 What is the capacity of middle? How much stuff can I store in its run? That you can 46 00:03:00,900 --> 00:03:04,900 see it has five elements. And the reason that comes about is when we sliced it, 47 00:03:04,900 --> 00:03:08,700 we sliced it from this thing and we slide from here. So this is, these are the 48 00:03:08,700 --> 00:03:12,500 five elements. So as I append more and more things, I will 49 00:03:12,500 --> 00:03:16,800 eventually hit that capacity. So let's just add some more numbers on here. 50 00:03:17,600 --> 00:03:21,300 And this is going to here and say middle equals up in the middle 51 00:03:21,400 --> 00:03:22,400 43. 52 00:03:25,400 --> 00:03:29,700 And to this. So we've added a bunch of numbers and let's 53 00:03:29,700 --> 00:03:30,800 print it out. 54 00:03:36,100 --> 00:03:39,300 So that's word perfectly nicely. Now, what's happened to the underlying array? 55 00:03:43,800 --> 00:03:47,800 So what happens the underlying array was? We filled it up to the 56 00:03:47,800 --> 00:03:51,600 point at which it could be filled and then after that 57 00:03:51,900 --> 00:03:55,800 we got some extra elements added on to middle. So middle actually had to change. So 58 00:03:55,800 --> 00:03:59,400 now, it's actually been extended. Now, what happens if 59 00:04:00,000 --> 00:04:04,800 the underlying array changes member that number one there I'm going to double it again 60 00:04:05,600 --> 00:04:07,900 and I'm going to print out these two items 61 00:04:10,800 --> 00:04:13,300 just do this and if you have a 62 00:04:13,400 --> 00:04:17,900 In here within middle and numbs is it's worth noticing. 63 00:04:17,900 --> 00:04:21,600 That previously, when I doubled an element in 64 00:04:21,700 --> 00:04:25,200 numbs, it affected middle because they're pointing to the same underlying array. 65 00:04:25,800 --> 00:04:29,400 But as I appended, I ended up in a situation where 66 00:04:29,700 --> 00:04:33,900 this 44 got doubled in the underlying array, but 67 00:04:33,900 --> 00:04:37,800 not in the slice because I append it past its capacity 68 00:04:37,800 --> 00:04:41,600 and I actually had to get copied and moved is that this is now, this is now 69 00:04:41,600 --> 00:04:43,200 become a different 70 00:04:43,300 --> 00:04:47,400 Slice. So, it's worth noticing that if you're trying to rely on, well, this slices 71 00:04:47,400 --> 00:04:51,600 talking to, you know, is a reference to something underneath, there can be situations 72 00:04:51,600 --> 00:04:55,800 in which that is no longer true, and it can be very confusing. So, in 73 00:04:55,800 --> 00:04:59,800 general, what I like to do is think about not, think about the implementation detail, 74 00:04:59,800 --> 00:05:03,800 just know that it's very fast for kind of normal circumstances. And if I 75 00:05:03,800 --> 00:05:07,800 really need this sort of effect that I've got to be very careful about the capacity. 76 00:05:08,400 --> 00:05:12,600 Now there's other sorts of things you can do with slices that 77 00:05:12,600 --> 00:05:13,100 are 78 00:05:13,400 --> 00:05:17,900 Not built into go for example, if you wanted to remove an element from the middle of a slice, 79 00:05:18,100 --> 00:05:22,500 then you really don't have a lot of choice, other than, to create something else. So for example, let's 80 00:05:22,500 --> 00:05:26,800 suppose I've got middle here, which has got these in and I suppose, I want to get rid of 81 00:05:27,000 --> 00:05:31,900 this 43. Well, there's a couple of ways to do is the simplest is to do something 82 00:05:31,900 --> 00:05:35,400 like this. I'm going to take everything up to that element, 83 00:05:36,000 --> 00:05:40,800 and then I'm going to take everything from that element plus 1. So it's just then a question of 84 00:05:40,800 --> 00:05:43,200 figuring out these offsets. So this is 0 85 00:05:43,300 --> 00:05:47,000 Oh one, two, three. So I'm going to go up to 3 and then 3 plus 1 86 00:05:47,700 --> 00:05:49,900 4. And then I was print out that 87 00:05:57,800 --> 00:05:58,800 I'm sorry, this is a 88 00:06:01,300 --> 00:06:05,600 There we go. So now 43 is disappeared from here. But once again this is 89 00:06:05,600 --> 00:06:09,900 essentially a new slice because I've changed as there was a data copy occurred 90 00:06:09,900 --> 00:06:13,900 at that point. So slices are great for simple operations when you're just working 91 00:06:13,900 --> 00:06:17,700 with them. Moving them around, they're very lightweight because it just a pointer and a 92 00:06:17,700 --> 00:06:21,800 couple of counters as you start to add to them. Then you and 93 00:06:21,800 --> 00:06:25,800 can end up having to reallocate, or move memory around. But in general, there are fantastic 94 00:06:25,800 --> 00:06:26,500 part of go