This course has been updated! We now recommend you take the Basics of Go course.

Check out a free preview of the full Go for JavaScript Developers course:
The "Slices" Lesson is part of the full, Go for JavaScript Developers course featured in this preview video. Here's what you'd learn in this lesson:

Brenna explains that a slice is a form of array in Go, demonstrates how to modify its length, and then goes over how to append an element to a slice.

Get Unlimited Access Now

Transcript from the "Slices" Lesson

[00:00:00]
>> So this is a slight example of what we just talked about, that fruit array is at the top with our five elements of strings, and then our sliced fruit which gives us back pear and apple is that second variable called sliced fruit. On the left hand side, those red boxes, those kinda represent how our spliced fruit variable is keeping track of its information.

[00:00:19] And then the blue boxes at the bottom are our representation of the actual array, the fruit array as a whole. So you'll notice that the indexes on the bottom 0, 1, 2, 3, 4 are talking about each index of each element in the array. And we said we wanted elements 1 to 3.

[00:00:35] Something I like to do to think about like, how those indexes actually organize themselves. You just slide those numbers to the like dividing area between the elements. So if you think like it starts at 1, which is the left of pair here, go until we're 3 is which is to the left of kumquat, you see pear and apple, which is the array that we're printing to the the screen.

[00:00:58] Cool, but so our pointer here is like, okay, I'm slice, I'm gonna carve myself out of an existing array. And my starting point, is whatever that first element of the array is. So my pointer where I'm storing my data starts at element 1, which is pear. And then I know that I'm a length 2, cuz you told me to cut out 2 elements, pair and apple.

[00:01:16] But because I'm associated with this underlying array, there's a couple elements of extra space. So my capacity is four. But every time we're talking about a slice, it knows about these three pieces of data and knows where it's starting. Where does my data live? How long am I?

[00:01:30] How many elements do I have? And what's the capacity I have to work with, before I need to allocate more memory. Hop back into the code and talk about how we modify the length of a slice. So right now, if our capacity is 4, and we wanna add stuff to it, how do we do that?

[00:01:50] If everything is like set up in memory, what are some methods we can use to get around the restriction there. And the first one we're gonna look at is append. We'll clean that up just a little bit. So let's say we have our fruit array. I'm gonna bring back a slice.

[00:02:12] Okay, we have a slice called a splicedFruit. And let's say we want to add. Fruit to our array. We need to use the built in function append, and append is going to take a few arguments. The first is, what is our starting slice. What slice are we starting with?

[00:02:31] And we're starting with splicedFruit. And then we can add on additional fruits that we want to append or slice. Cantaloupe, with an e. Cherries, so if we were to add cantaloupe and cherries to our splicedFruit array. And let's go ahead and print out all of the information of.

[00:03:04] SplicedFruit. And I'm also gonna print out fruitToAdd. So we're gonna look at both of these slices in the console. So you'll see our first one there just has the 2, like we saw before. And our second one now has the 2 that we sliced out of the array, and the additional 2 that we just appended on.

[00:03:31] Just for kicks, let's go ahead and print out. The length of our new array. And we'll also print out the capacity. Another interesting thing to note, is that FruitToAdd here isn't just giving me back the two elements I added on. Slightly poorly named variable, there should probably be total fruit or something along those lines.

[00:04:03] But it's gonna give me back the original array and anything I've added to it, and give me just the whole slice back of everything that I want in there. So I print out the length and capacity of FruitToAdd. You'll see that they're 4, and then the capacity is 4 which makes sense, right.

[00:04:19] Cuz we're working with that array that had a capacity of 4 to begin with. We filled up all 4 slots. Goes like cool, I've allocated all the memory, we're good to go, let's move on. What happens if I add a third element here. Lemon. So now we're reaching five elements in this array, even though our capacity said four to begin with.

[00:04:39] So I'm gonna print this out again. So no we've got a length of 5 and a capacity of 8, which seems curious. So we started with. Let's go ahead and print out the same information for both of our arrays. And then let's do splicedFruit here. Cool, so our first line of console logs there is we have pear apple, and then pear, apple, cantaloupe, cherries lemon, the two different slices that we're talking about.

[00:05:31] The second output says 2 and 4, so we're saying the length of our first splicedFruit array is 2,and the capacity of our splicedFruit array here is 4. On the next line, we're saying that the length of our total fruit array is 5, which makes sense, but the capacity is 8.

[00:05:54] So you'll notice that Go hasn't just been like, okay, cool, I'm gonna add two additional elements to your array. I'm actually gonna double the capacity that I had, based on the slice that you passed in the first place. So if our capacity of splicedFruit was 4 which we see here, when it's like, I don't know how much more memory you want, it takes the capacity of that array we're appending to and doubles it.

[00:06:15] And it's like okay, now take up as much space as you want. I have now doubled your array length. And then if we continue to add more elements here, it's gonna allocate as much memory as necessary, by using the capacity of our original right here.