Golang Tutorial #13 - Slices

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everybody and welcome back to the golang tutorial so in this video what we're gonna do is talk about slices now slices are kind of like an addition to arrays they implement some functionality and fix some problems that we would have with standard arrays and the most common example of a problem that they solved for us is that with standard arrays we need to determine the size of that array and pick the size of it right when we create it now this is a problem because sometimes we don't know how many elements are gonna be in the array maybe we'll be adding things in the future removing things later on so it's really difficult to pick how big the array should be so in this case what slices let us do is take portions of an array which we'll see in a second and also define the fact that hey we don't know how big this slice is gonna be until we get it or something like that and it will actually allow us to change the size of the slices as we add new elements in remove new elements all of that we'll see that as we go through but I just wanted to give you an overview of what slice is really do for us so I'm gonna go to the white board here and I want to start explaining slices by really illustrating to you how arrays work in golang now this is important because if you don't understand how arrays are represented then slices are gonna be even more difficult to understand so I'm just gonna go through that here okay so let me say that I draw an array here we'll just say that this has four elements inside so its length for the first index here we know zero so zero one two three let's say this has a bunch of numbers in it so like 0 3 6 9 will discount it by 3 and this is our array now the way that arrays are actually represented in golang is by three things we have what's known as a pointer which is simply the location of the first element in the array and then we have what's known as a length which is simply the size of the array and we have a capacity which is always the same as the length so the pointer points to the first element in the array the length and the capacity are just some digit that tell us how many elements are in the array so in this case it tells us for now the reason the the only things we need is because if we know the location of the first element in the array to get the other elements in the array all we have to do is kind of move to the right and move to the right and move to the right all the way up until we hit the length of the array and we can access all of those elements so if we access index to all we need to do is add the value two to whatever the location of the first element is and we can easily grab that element so I hope that makes sense but just understand we have a pointer a length and the capacity and the length and the capacity are always the same whereas the length is telling us how many elements and the capacity is telling us the maximum amount of elements we could have so that is how rays are represented and kind of work in golang very important to remember length and capacity always the same for arrays okay so now we're gonna move on to slices so actually I should probably should left that array there but let me draw this array again I'm gonna talk to you about what slices are so a slice is really a portion of an array so in this case we could say that this box that I've just drawn here is a slice of this underlying array now slices are their own data type and go lengths they're different than arrays but they kind of work on top of arrays as we're seeing here so how we represent a slice is the exact same way that we represent an array except there's one thing that's different so we have a pointer which tells us the location of the first element in the slice and this location is actually gonna be the location of whatever element is in that underlying array now I know this seems weird but like if the slice started here then the pointer would be the same as what represented that underlying array hopefully that makes sense but anyways a pointer to the location of the first element in the slice and then we have what's known as a length and a capacity okay so it's hard to write too small with this pen but you hopefully you guys can read that all right so the length and the capacity for a slice are actually different the length is the same thing that we saw in arrays it's the number of elements in the slice so in this case the length would be two because we have two elements the six and the three but the capacity is actually how many elements we could have in this case it would be three because if we start with the slice at this position six and we actually could have a length of three if we decide to extend the slice out by one more element so that is kind of what we mean by capacity it's how many elements are left at the end from the very beginning that we could have in the array right so if I put this now to twelve then the capacity would change to four so that is what we mean by capacity how many kind of extra elements were I guess how many elements after that first pointer so in this case there's four but if we remove twelve then obviously we go back down to three so I'm hoping that makes sense but that is the difference between a slice and an array and I'm gonna show you how we make slices now and and how those work but just understand that they are portions of an array and the length and the capacity is an important thing to understand because if we take a slice something like this what we can actually do is what's known as Reese lice it and extend the slice to have up to the length of the capacity elements inside of it so if we start with length two but the capacity is four I can Reese lice this slice so I'd actually take a slice of this slice to extend it by a few elements um so hopefully that's making sense but let me put away the whiteboard and then we'll get into coding okay so let's actually get to coding now so you see up creating an array this is an array of length five also has capacity five and contains these elements now I'm gonna show you how we can take slices of this array and show you what the slice operator is and how about works in this program so keep in mind that slices and arrays are different they are a different data type that's an important thing to understanding golang but I'll show you how we can actually make a slice of this array so how we can grab some elements of it so I'm gonna save RS as standing for slice and then what I'm gonna do is put brackets I'm gonna put int and notice how I didn't put how many values I was gonna take inside of these brackets when you don't do that what that says is this is a slice so if I don't put the amount elements that are gonna be inside of my array then what that means is that this item or this object will be a slice okay so we said VAR s which is a slice is equal to and now I'm going to take a slice of X so I'll put X here I'll put brackets where I would usually index some values here but instead I'm gonna use the slice operator now the slice operator actually don't know if this is the formal name but this is what they call it in Python at least here is simply 1 : so if you put a colon inside of the brackets you are denoting that you are taking a slice of this object whatever is on the left hand side in this case X now the basic way that this works is you can put numbers on the left and right hand side of these of this colon and that tells you what index is to grab from the underlying array so in this case if we leave it empty we don't put any values on the left or right hand side this means just copy the array so it means start at the beginning and go to the end include the end that's what it means when we don't leave anything on the left and right hand side in fact if we don't leave anything on the left that that means we don't put any number sorry not don't leave anything I don't know is saying there we don't have a value on the left hand side of this cool and what that means is start at the beginning if we don't have a value on the right hand side of this colon that means go to the very end so if I put one colon here what this is saying is start at one go to the very end if I put 1 3 here this is saying start at 1 go to 3 but do not include 3 so that means that what I would actually grab here is the value 2 3 because I would go to index 3 which is the value 4 but I wouldn't include it in fact let me just print this out to you and we'll do some examples and I'll show you really how this works so if I print the value of s I'll go run tutorial dot go we get to 3 again because we start at 1 we went to 3 and we didn't include 3 now let's have a look at the length and the capacity of this slice and try to take a guess at what the capacity is because that's something that we were just going over so go around tutorial let go we get the length of this is 2 so to get the length of a slice or an array for that matter you can use the Len method or Len function Sri simply put the object inside of lens so inside of the brackets it will tell you how long it is ok so now we're gonna look at capacity now capacity we can get by using cap so the capacity of s should be what well let's have a look at what it actually is and we see that we get 4 so notice of the capacity isn't different than the length and that's because if we look at where we started at one so that would be right here value two there is four elements that we could possibly have in our slice so you're like well what's the point of having the capacity why do we need that well that's because we can actually resize our slice now this is gonna seem weird but what I can do is put s : so inside of here : and then put capacity of s like that and what this will do is extend our slice so that it is the full length of this array so it starts out whatever this element is and then it just extends and gets all the other elements that were in there so in the capacity in the underlying array so doing this extends the array and adds on those other elements so let's look at this let's go go run tutorial let go and we get 2 3 4 5 so we've successfully reese lysed our slice and extended it so of course we can take slices of slices right if I print out slice starting at 2 : let's actually see what we get so index 2 I think we'll actually crash let me just start at index 1 and let's see what this does for us so if we do index 1 we see we just get the value 3 so we successfully sliced our slice that made a new slice that still has the pointer to the underlying array of X here but it's just at a different position right and that's the basics of how slices work and how you can take these slices now I'll show you how we can make our slices from the get-go so rather than having to have an array like this maybe we want to make a slice maybe we want to add stuff to a slice there's a lot more things that we need to go over still so let's actually make a slice so we can do this a few different ways I'll show you the exact like I guess explicit way first and then the implicit way after so let's say VAR a let's put brackets int and notice that you can do a slice of strings you can do a slices of like a slice of slice if you wanted to whatever you want you can do a slice of pretty well anything but let's do a slice of int and that's equal to slice of int literal which means now I can just type the values that I want inside of here now it doesn't matter how many values I have because again the slice when we do it like this it doesn't have a predefined amount of things that are inside of it so this has successfully created a slice of intz and these are the values that will be in the slice now the way this actually works is it will create an array first that is of size whatever the amount of elements are that we put in here then it will just simply have a slice that slices the entire array so it's kind of like cheating a little bit when you do this you're making a slice to start but really what's happening first is you're making an integer array in this instance that will have this many elements inside of it so that will be the capacity and the length of it and then your slice is just that entire thing so that's how that works so I can actually prove that to you by go fmt dot println and i'll print to you the capacity of a and you'll notice that it will be the same size as the amount of elements we put here so if we go ahead and we look at this go around tutorial go we get a capacity of five so if I take a right and I slice it so we slice it up to three so we start the beginning and we slice the three and we look at the capacity of a so let's do that so the capacity of that new slice we get the capacity is still five right because it's pointing to that underlying array that it created when it started okay so that's how we make the slice now I'm gonna show you how we can add elements to a slice which is pretty useful so what actually happens is you can't truly increase the size of a slice what we can do is make a new slice that has the same values but increased so there's a function called a pent and then what the append function takes is a slice as the first argument and then an element that we want to add to the slice as the next so in this case I'll add that I want to put the value ten into my slice and what this actually does is return a new slice that is this slice kind of plus this slice so append means add to the end so what would it would look like is this so we'd add ten to the end of that slice so let's actually have a look at what this is so I'm gonna say B colon equals append a comma 10 because this returns a new slice so I need to store it in a variable which I'm gonna do here and I'll say fmt dot print line B so let's look at what B is and we get 5 6 7 8 9 10 so we appended that element to this slice so we couldn't truly extend the slice we couldn't change a but what I could do is say a equals append a 10 what that will do is say a is equal to a new slice which is the original a plus the appended 10 so that's a common way to do this too if you want to have the same variable name being extended that works fine but we of course since this returns a new slice we can assign it anywhere we'd like so that's append and those are the few different ways that you make slices now we'll be dealing more with slices as we go through this tutorial series the last thing I want to show you is how we can make slices with the keyword make or with the function make now we're gonna use make a lot especially when we get into concurrency but there's something called make so I can do something like a colon equals make and then what I do is I put the type that I want to make so in this case I want to make a slice of intz and the size or the capacity that I want it to be so if I do this and then we go fmt dot print line a let's have a look at what we get go around tutorial Co we get all zeroed out in int array are certain that int array in slice of size 5 so we can do many things with make we'll see this more in the future but I just want to show the Sukkos this is a way that you can make a new slice as well rather than just typing it out if you wanted to make a slice that's empty but a certain size or length to start then you can simply just put int and then put 5 like that and it will make that for you now in fact let's actually just look at what the type is of this so let's go percent capital T pull an A and have a look at what this is oops go run tutorial dot go and we get a type int right so I know this is not a very descriptive type this is telling you this is a slice not an array because if this was an array it would have a 5 inside of the square brackets before int ok so I think I'm gonna wrap up the video here this has been pretty much everything you really need to know about slices at least for now we'll be using slices and a lot of examples and things in the future I hope you guys enjoyed if you did make sure to leave a like subscribe and I will see you in the next tutorial
Info
Channel: Tech With Tim
Views: 19,576
Rating: undefined out of 5
Keywords: tech with tim, golang, go programming language, golang slice, golang slices explained, slice go, slice golang, golang slice vs array, golang arrays and slices, slices golang
Id: KzKNGGoaT5U
Channel Id: undefined
Length: 15min 31sec (931 seconds)
Published: Thu May 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.