Check out a free preview of the full Go for JavaScript Developers course:
The "Make" 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 how the built-in method make works, and how it allocates memory.

Get Unlimited Access Now

Transcript from the "Make" Lesson

>> So needing to know the exact length of an array and everything that's in it every time that you instantiate one that seems problematic, which is what brings us to the slice, and the slice is gonna feel a little bit more familiar to JavaScript in terms of how we implement it.

[00:00:11] But there are some really interesting things happening behind the scenes. So a slice essentially is a segment of an underlying array. So what's happening is go is actually allocating some memory for us, and then it's telling us okay, I've carved out this much space for you behind the scenes.

[00:00:26] I know you're only using this section, but I still need to put it somewhere in your computer. And so it stores it as if it's in array, it just doesn't tell you about that part. So we're gonna look at what that looks like. And then there's a built-in function called make that's crucial to actually instantiating a slice so that it knows two carve up memory in the first place.

[00:00:43] So the make function that we'll talk about first according to the docs that actually says it initializes and allocates space and memory for a slice, map, or channel. And this is important [be]cause it doesn't include an array and it's because it needs to know how much space it needs to allocate, and since we're not specifying a length it doesn't know what to do here.

[00:01:02] So let's hop over to slices.go and talk a little bit more about what makes does and how to instantiate the slice. Okay, so in our first example here we have our main function and at the top I have two different variables that have been instantiated. One is called myArray and a set two and a five element array of integers.

[00:01:31] And on line eight, we've got a mySlice, and the only difference between the two and we're instantiating them is that the second which is a slice doesn't have that length in there. Cool. So if I run this code as is, you'll notice on 10 and 11, I'm trying to establish that the zero with element of each of those things has a value of one, and then line 13 and 14.

[00:01:49] I'm just printing out what those values are. So if I run the code as is we are in section four code, and then slices. You'll see I get an error. And the error says that there's an index out of range zero with length zero and this is happening on go line 11.

[00:02:10] And so here's where make comes in. So my array knows that hey, go carve out five sections of memory to store integers in, and so if we were to run. My array everything would work fine. But because we don't have a length here go doesn't know how much space to allocate.

[00:02:26] So in order to fix that, we have to use the built in method make an Intel go, okay, you might change this later, but how much memory do you want to allocate right away? So to do that. I'm just gonna modify this first example here two on the left hand side assign it to an initial value.

[00:02:44] An initial value is gonna start with a make built in function. We know we want it to be an array of integers and we want an initial length to be five. We run our program again. You'll see that the two print out just fine, that both of them have now allocated that first element to have a value of one.

[00:03:04] The arguments on the right hand side, it can go up to three. So, our first set here is that we're instantiating a slice and it has an integer as its set of types. The second argument is the initial length of that array. And it does take an optional third argument.

[00:03:18] That's the maximum capacity that that array can hold. So if I just print the program as is. Let's see. You'll see that right now our slice, that second output printed there still has only five elements. But similarly to how we used len to find out the length of something.

[00:03:46] So if I say len of my slice, you'll see that number five. But I can also use a built in verb, cap for capacity, to ask my slice, okay, you're five elements long now, but how long can you be in the future? You'll see that prints out 10.

>> Why is an array is not 10? Is that-
>> On line 8?
>> Yeah, in terms of capacity, why is it 10?
>> The initial amount, let's say you know you're going to be modifying the array in the near future. I believe my understanding is that it optimizes how much space is available for immediate modification of the array, we'll see a little bit later that we can use functions like append, which will tack on extra memory, so let's say you have a capacity of 10 and you have 12 elements, it will allocate more memory when it needs to, but I think it's just a way to be like, hey, this is carved out ready to go for you without having to re establish that behind the scenes.

>> You mean my slice right?
>> I mean slice? So all slices are arrays behind the scenes.
>> Yeah.
>> But right now, this particular slice is only pointing to a subset of a longer a behind the scenes. I've really great slide for this in just a moment [LAUGH].

>> So if you did the cap on the my array with just get five.
>> Let's check it out. [SOUND] Cool. So let's look at establishing arrays and slices a little bit differently. I'm gonna wipe out kind of the code we have here. And let's start with a variable I'm gonna call a fruit array.

[00:05:57] And I want that to be an actual array a carved out slice of memory that has five elements of strings. Using the curly braces to set some initial values. So let's throw in a bunch of fruit here. [SOUND] Cool. So we're working with a fruit array of five strings.

[00:06:25] If I wanted to establish a slice off of this array, I could set it to Something like this, where I'm saying I'm going to grab a section of the original array which is called fruitArray. And from that fruitArray, I wanna grab just a subset of the elements. And so this 1:3 is saying, start before element 1 and end before element 3, which is gonna be a length of 2.

[00:07:00] So I can go ahead and print line here. And now we see a subset of that array which is just pear and apple. What's interesting is if I asked my spliced fruit slice for its length and capacity, And I print those two values here. We get different values.

[00:07:36] So the length is two which makes sense we see two elements in the array, but the capacity is four. And so because each slice is associated with its underlying array it has to be allocated somewhere in memory. It knows it's attached to this fruit or a guy because we're telling it to pull the slice off of that variable.

[00:07:52] And if we start here and count to the end of our array, we have four available elements to work with, even if we're only using two. The link might be two, but our capacity of memory for this particular array is four, which is a super interesting aspect of go when it's talking about slices in arrays.