Golang Slices - "ArrayList" in Go

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] slices are similar but still quite different from the race we talked about erasing our last video arrays are like a few already built houses and slices are more like a giant piece of land that you plan on building many many houses obviously you can build one or two houses or you can build an entire neighborhood more technically speaking arrays are a fixed size amount of elements that you allocate from the stack and slices are a growing number of elements that you allocate from the heap if you don't know what stack and heap are make sure to leave a comment in the comment section down below i'm making another video about it if i have to make it comparable though i would compare slices and go to maybe a waitlist in java that i said we're going to talk about slices in today's video how they look like in go and how we can use them here we have a hello world program open in egolan which is an id by jetblin if you don't know how to create a hello program make sure to check out my previous video and if you remember from our last video how to create and fix size array is by having these brackets and three dots and the actual elements that you want to assign into the fixed size array so this is fixed let's call it fixed you can use this effects either way perfectly fine but if you want to append elements into this array you would not be able to do so because if you try to reassign it with a an array of a different size unfortunately you would not be able to do so because it is going to complain saying that hey it hasn't been declared previously as an integer away of the three elements and you cannot assign it with two elements that's why we have slices in kill so if we try to we do a fresh declaration of slices and get rid of these three dots and this is how you would declare a slice and go so if we go ahead and print this out as you can see it is the first one is going to be the fixed size away here and then the second one is the slice i thought we have i just i created like a raiser we are able to print out the length of the slice by calling the building function of golem so if we hit ctrl and click on length we will be able to see the implementation of length so if you pass a slice or map it is going to return the number of elements for the parabola passed in in this case it is going to be the number of elements for this slice which is two because we only have two elements so if we go ahead and print this out it is indeed two here now we can also reassign it as a slicer with a slice of a different size and that is going to work if we print this out it's going to give us a different slice zero one two instead of a zero one here not only that we can actually do a pen operation on the slices so if we actually append a different element into it and then we can append one or more elements if we do this and then print out the slice again we'll be able to see that we have more elements in the exact same slice now something slightly different about slices is that if we actually try to print out capacity and the length of the slice they may or may not be the same because the length is going to be the size of the how many elements that there are in the slides we actually go to the implementation here so if we see slicer the number of elements in the slice passed in but if we actually go see the uh implementation of a capacity instead if we pass in the slice it is going to be the maximum length of the slice that can reach before we have to resize it or in other words grow the size of the slice typically speaking doubling the current capacity is the growing strategy for example if you have a slice with a 300 element and you try to append one extra element into it then the slice it typically grows its capacity into 600 instead of 301 but not always the case along with that we can actually declare slices by using the function and make the built-in function and make and we just need to pass in the type of the slice and then we need to pass in the capacity how many elements that we want to allocate from the heap so if you actually click on this interpretation here and it's going to this is a built-in function in go that allocates and initialize an object of a type slice because we are passing in uh the integer slice as the type and we can go ahead and print this out and as you can probably already guess all of the elements because we are not explicitly defining them or assigning value to this slice it is all going to be initialized it to zero because zero is at the zero value for integers aside from that we can actually get a slice of face slice so what do i actually mean by that so if we actually just come in here and make a slice of a slice so what that means is as you all already know that we have previously declared a variable called a here which is a slice and that contains a value of zero one two four five six actually let's fix that we missed one element here all right so we have all seven elements in this slice so this is the same thing so if i actually do this this is the same thing as zero to element seven because we have seven elements so if i actually print this out it will give us the exact same thing because what this is telling us is that we are getting from the index of a zero to the elements right before the index of a seven and we can be a little bit more um it's it's kind of flexible here because you can go past it but uh the compiler would still know that hey there's not enough elements so i'm just going to append an extra element into it and now if you try to print out the length of a it is going to show us and as well as the capacity it is we are going to show us that the length of the a is going to be eight because we have a zero to this is seven elements plus the additional one that we just implicitly appended toward towards the end of the slice that originally had seven elements however if you are regretful of this decision you can do something about it so you can actually reassign a two zero seven here so what this will do is again get rid of the very last elements that you just appended it towards the end because you are telling the compiler that hey i want the range from 0 to 7 for array a and we assign that to a instead of from zero to eight and this is what exactly what that is doing here and another thing is that we can get rid of the ending index or the starting index so if we actually do this here now what this one it will do is it will just try to figure out what is the length of the slice and then put that here and what this will do is it's just going to put the very starting index which is zero implicitly so if we actually print this out it is going to give us the exact same elements aside from that if you remember from our previous video where we talked about variable declaration we can actually make the comparison between slices with null because slices are of reference type so if we do the comparison here this is not going to the compiler is going to accept this compared to if you actually try to compare a integer with null this is not going to work the compiler is going to complain because it doesn't make any sense here because an integer would never be not because the integers zero value would be zero instead of null for slices and any other reference type you can also declare a slice using this format instead instead of the colon equals here that will implicitly assign the zero value to this slice here and if we actually try to print out equals to no here this one is always going to give us a true because we are implicitly assigning null to this slice here like we talked about no is the zero value for all reference type that wraps it up for our tutorial video on the slices in a go if you want to learn the gopro language but you hate reading technical books this channel is for you because i'll be doing all that work for you with that let's shout again in our next video you
Info
Channel: Golang Dojo
Views: 766
Rating: undefined out of 5
Keywords: golang, golang 2021, learn golang, go lang, golang in 2021, go language, golang tutorial, go tutorial, go programming language, golang tutorial for beginners, golang crash course, golang for beginners, Golang Slices - Understanding Slices in Go, golang slices, slices in go, go programming language slices, golang slices explained, golang slices tutorial, understanding slices in golang, slices in go programming language, learning golang slices, learning slices in go, slices
Id: NnBKdUu1Q0U
Channel Id: undefined
Length: 9min 22sec (562 seconds)
Published: Sat May 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.