Learn Arrays and Slices in Go | GoLang Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on my name is james from nerd academy and in this video we're going to learn about arrays and slices in go so let's get into this arrays are probably one of the most used data structures throughout programming they're quite useful they're versatile they hold a bunch of data and things that you need to use in go and in most programming languages arrays are a fixed length of sequence of elements languages such as c and c plus and java and go all the items in the array have to be of the same type so if you want to store some numbers the array has to be all of the same type of numbers so like integers or floats or whatever in languages like javascript and python the arrays can pretty much just hold whatever i mean you could have a string a boolean value a number a character oh just whatever kind of the wild west out there slices are a bit like vectors in c plus plus or arraylists in java slices they can grow and shrink and they're just kind of versatile whereas arrays they cannot grow or shrink the size is determined when they are declared and initialized but before we dive into slices we need to understand arrays just a little bit more arrays in most programming languages are technically just a pointer to some location in memory and that location is known as a contiguous part of memory that contains all the elements that kind of makes up the array which is why languages like c actually have a pointer arithmetic but it might be easier if we take a closer look at some kind of illustration so let's go take a look so let's make a very kind of basic array to get an understanding we'll have an array without straight lines all right we'll have four four items in our array we'll have zero we'll have two we'll have four and then eight so each index in our array is one two and zero one two and three so the first index note array started at zero is zero so when we declare we'll just call this array this is a pointer to that location in memory where that first item zero is at so let's say these are integer i don't know 16 whatever so the since this is contiguous this is all laid out next to each other in memory so from 0 to one it is the amount of memory that an n16 holds so whatever this memory is zero is to get to one it's plus one of that size and so on and so forth to get to two it's another plus one from one or plus two from zero if you will so when we access each element it is technically zero plus whatever the index is to get to that element so to get to three it is the memory plus three times the uh memory space that an n16 holds in the case of our little ray here all right let's open our code editor and kind of look out how we can initialize and use arrays all right let me show you the basic way to declare an array we use a keyword var we'll have a name and then the type so we'll do an array of three and ant so the brackets are required to specify that we're going to have array and then the number in here tells the compiler we're going to have array of three integers in go when you declare a variable it automatically initializes to whatever the zero value is in the case of a array of integers each each element will be you know initialized to zero so if we print our array out we should get a bunch of zeros let's go see what it looks like three zeros there you go now i talked about getting access to elements using like memory arithmetic or point arithmetic fortunately we don't have to do that in uh go you really don't even have to do it in c because you can just use the uh the brackets the index uh access using the index to access it so array one at zero we're going to get zero because we haven't done anything with it so zero and if you wanna get the last one you can go in the array and then we're gonna use the brackets then we're going to get the length of the array and then we're going to subtract that by 1. so if we were just get the if we just use the length which is 3 that's going to give us index 3 and that doesn't exist so in in our case here we need two so we take the length minus one that gives us two that gives us the last elements in the array so print this out we're going to get zero again but you should see there there it uh that gets last if we try to get three let me show you visual code visual studio code is actually telling me there's an error it's out of bounds it's out of the past the uh the memory addresses in our little array here so let's go ahead and comment that out out of bounds all right we can also declare and initialize the values at the same time so we'll make an array too we'll make this of size three int and we'll set that equal to three inch and we'll open up curly braces and then we can put in three integers in there and we can print this one out see array two one two and three another way we can declare an array is with the colon equals and we just do three we'll do four this time ins start at four five six and seven and if we print this one out we'll get four five six and seven there we go but i do want to point out here erase and go when you declare an array of type three in like this it is different than the four inch so there's four ends in here even though they're both array of integers they are actually not of the same type so we can't even we can't go here and say hey array three we want to set that to array two now see we're getting a little warning here because uh it says incompatible assign so we can't it's just not of the same type so let's comment that out here and finally there's kind of like a shorthand to create an array we can use the brackets and we put ellipsis in there that's three dots type int so this will as we'll have to put in the values but how many ever values we put in there it tells the compiler so if we put 10 values in there this this array will be of 10 integers the length and the capacity so instead of putting all the values in there i want to show you a little kind of secret that you can do so let's say we want a ray of at least five and we want the last one to be negative one so what this will do is make an array of six items the length will be six and the last item will be negative one the first five will be zero here let me print it out so you can see array four let's print this out five zeros and then a negative one we can also just keep going with this so this will make the first five zeros and then the last four will be negative one 1 0 and 1 respectively let's print this out again so that's kind of neat so if you want an array of like 100 items and then you want the last item to be something you specify that's one way you can do that so there's a built-in length there's also a built-in capacity which will make a little bit more sense we get to slices but we should just talk about it here so there's length and capacity so length of our array four and capacity of our array four we'll just go ahead and put that in here so it's length len is a built-in method and we pass in our array in there and then capacity is cap that's a built-in method and we pass in our array now in our case in an array four it should be nine and nine so there you go length 9 and capacity 9. all right now that we kind of got a better idea of arrays let's talk about slices now now slices are variable length sequences that are all of the same data types kind of like arrays but the key difference is variable length now a slice is written with the open and close brackets and your data type without the need for ellipses or numbers so you can declare a slice without knowing how many uh items are in there it could be just with nothing or you can use like the slice operator if you come from python that might look familiar you have your variable name you have your brackets and then you'll have a number colon number so that will kind of like slice out of your array and on that note you should know that arrays and slices are very much connected slices have access to the elements of an array this is known as the slices underlying array so a slice has three parts it's got a pointer it has length and capacity the pointer is pointing to the first place in the underlying array that the slice has access to the length is the number of elements it has this cannot exceed the capacity and the capacity is the number of elements that this uh slice can have so length and capacity don't have to be the same but in arrays they will be the same that's the that's the distinction there so let's head over to our handy whiteboard and take a look at another illustration and erase all this start over boop and go so this time we're going to do kind of like stacked up a little bit here so let's make a bigger array without straight lines oh my goodness so yep we'll just have a few items in here so once again we'll do 0 2 4 8 16 32 and 64. so here's our array we got item one element element zero element one element two three four five and six so let's say this uh array is just you know array whatever okay so we want to take a slice from this array we we name our slice and then it's brackets and let's say we want to take between two and four so we want four eight and sixteen so that's two colon four so our slice will then be just 4 8 and 16. different color here so our slice starts here and ends here so we get our slice here let's change the color so our our pointer of our slice i'll put a little asterisk here points to the first element it has access to and we'll change the color again so the the length is going to be 3 so we have 3 change the color one last time and the capacity is also three but since this is a slice this could actually grow we could actually change the the size of the slice but for now it's length three and capacity three which has just has access to the underlying array that we made made up right there all right let's hop back in our code editor and play around with slices for a second so let's make a an array of months so this will be an array we'll use ellipses it'll be of type string and we'll do january february march april may june july we got august september october november and december so let's say we just wanted the summer months so summer set that equal to months and we take a slice we'll go five to eight so that should print out june july and august print that out all right let's bring back summer all right june july and august now let's say we wanted i don't know quarter two we need quarter two so we take a slice from our months should be between three and six so let's print out q2 april may and june quarter two and there's something else we can do let's call a slice all so all the months if you want everything from the underlying array if you wanted a slice you just put in a colon there so if you just leave no number to no number it's going to take the first to the last so let's print out that all right get them all we can also just make a slice without any underlying array so we do that with the built-in method make to make a slice make we'll do a type int again and we'll have it of length three actually hold on let's do two that's the point i want to make so just like an array we can access with 0 and 1 and etc so we'll just set that to 1 slice of 1 will be 2. so now if we print out this slice actually let's make a reusable method we'll call this funk print slice and we'll take in a slice we'll call a slice and we'll take in a slice so let's do a print format we'll say slice percent v length percent d and capacity percent d put a new line in there that's in our slice we'll take length of our slice capacity cap of our slice okay so let's print slice print our slice there we go go run main dot go there we go slice one two length two capacity two so we have capacitive two how do we make it bigger we use the built-in method append so we take you can you can actually call it anything else but we'll just keep the same slice name so we can do slice one a pen slice and then we can add a value to it or we'll just keep the same slice actually grow it and append the value three so let's print our slice out and rerun it so now our slice is one two three length three and capacity four all right let's let's add a couple more to it let's add four and five so you can just add a comma separated list of values in here as long as they're the same type and it will append those to our slice so let's print our slice one more time and see what we get so now our slices one two three four five length of five and capacity eight i hope you noticed that trend there there's a reason i started with two in most programming languages that have kind of some kind of that flexible like this are slices that can grow like a vector or something um usually the underlying array you'll you'll start with let's in our case we had one and two so the array was a size two then we wanted more we wanted to add one to it so what they normally do is multiply they'll make a new array of our current size and multiply that by two the underlying array will be now of size four then we added some more and it was like well we're past four let's double it again so now we end up with an array underline array of our slice of capacity of eight so that's just kind of like a normal thing that they do if you don't know and i would be remiss if i didn't at least kind of show you how to use a loop and go through your arrays and slices and do things that you need to to the values so we just use a for loop we'll do four i equals zero i is less than the length of our slice and then i plus plus that's the post kind of action what we do is we initialize the value i to zero usually i is for index as long as i is less than the length of slice less than not less than or equal to because if we did less than or equal to we would actually get that out of bounds issue and then whatever happens in this in these curly braces we'll do we'll do something with and then at the end it'll increment i so we'll just print our slice and the value we'll use i for the index that'll go in the first percent d here and then we'll actually just get the value at that index go run that again ah need a new line makes it look prettier so one two three four and five and the indices and the values now maybe you don't like setting up that way well fortunately for you there's a four range loop so the range keyword will take our an array or a slice or a map or something and we turn like the index and the value we'll set that equal to range and our slice so much like our for loop we have our index which was just i and then our value we can access with our index so a nice thing about this if you actually don't need the index you can just use an underscore and we just kind of disregard that uh that value that we're getting from this right here so we can just print our value out and run that again so it's up here one two three four five awesome all right well i can't just leave it at that so let's have a little bit of fun and let's reverse an array so we'll take an array actually i'm taking in a slice but we can still use it to reverse an array so we're going to do a for loop we're going to have i and we're going to have j normally that's what we do we just have i and then if you're going to have like multiple nested loops you'll start with i then go j then k et cetera we'll set i to zero and then we'll set j to the length of the array minus one so much like python you can initialize variables in one of one go just using commas so i will be zero and j will be length of array minus 1. so as long as i is less than j we'll continue this for loop and at the end we'll set i and j we'll set that to i well increments i and then we'll actually doesn't like that we'll increment i and then we'll decrement j see if i can get this right so i will increment and j will decrement so basically what this is doing we got an array i is starting at the beginning and j is starting at the end and then as we loop through our array i increments and j decrements until we get we'll meet at the center so we'll use a kind of nifty inline so we'll take the array at i and the array at j and we'll set those to the array at j and the array at i so what this is effectively doing is reversing our array obviously so each time we iterate we're going to take the value of j which is at the end and put it in the value of the the element the index at i which is at the beginning and then we'll take the element that's at the end and put it at the beginning until we get to the middle we're not going to keep going because once i once i is about to go past j that's when we need to stop otherwise it's not going to be reversed you'll have duplicate values in there let's reverse this slice we have over here and let's reverse let's print it out so now it's five four three two and one but we should probably make a quick array set that equal to we'll use ellipses here type in curly braces we'll do six through ten reverse the array and then we'll print that out oh but we're getting an error here that's because it's asking for a slice but if we recall we can take our array put the brackets in there and then put one colon in between the brackets so this is actually a slice of the entire array so we're going to reverse our array but our method is requesting a slice so we're just going to pass we're going to pass the entire slice the array as a slice to it all right so let's run that and we should have two yep two reversed arrays well two slices that are reversed and there you have it that's the basics of arrays and slices and when i first started i was actually sort of confused about arrays and slices because i really thought that slices were just another name that go used for arrays kind of like python has lists that are just another name for arrays so i was kind of confused at the beginning but there are actually two different kind of data structures one is a fixed type your raise and then your slices they can grow as you need to add more items to them and if you made it this far in the video i really do appreciate it i thank you a lot so do me a favor and hit the like button help out the algorithm if you will and if you want to watch more about go just watch this video right here and we'll see you in the next one [Music] you
Info
Channel: NerdCademy
Views: 100
Rating: undefined out of 5
Keywords: software development, software development tutorial, programming tutorial, golang tutorial, go tutorial, arrays in golang, arrays in go, slices in go, slices in golang, arrays and slices in golang, arrays and slices in go, go programming tutorial, golang programming tutorial, go arrays vs slices, golang array vs slices, go array length, go array capacity, golang array length, golang array capacity, programming, golang reverse array, golang reverse slice
Id: AxaeOSo9Y_c
Channel Id: undefined
Length: 21min 16sec (1276 seconds)
Published: Mon Dec 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.