Welcome.
Until now, you have created slicers only by using literals.
Actually, there is another function called the make.
It allows you to allocate the back injury or fossilise with the given length and capacity.
So why there is a function called the make is because a slice doesn't have a defined length up front.
Right.
So the make function allows you to create a slice with a defined length for you.
But don't be mistaken that its length becomes fixed.
You can still open to it.
Let's take it out.
The main function is just another building function so you can call it from anywhere.
You just need to type make and then tell it which type of a slice you want to make.
Then you need to tell it the length of the slice that you want to create.
This second argument also describes the capacity of the back injury.
So the return slice, this length and capacity become equal.
That's it.
Then the make function will do the rest and return you a slice like this one here.
It creates an insulin's with three empty elements and returns it back.
As you can see, it sets the elements to their zero values.
You might be asking why should I use the make function?
What I can create slices using a less literal already?
Well, as you know, when a slice needs to grow and if there isn't enough capacity, the append function
allocates a new back injury.
And as you know, that's an expensive operation.
So you can prevent it by allocating a large enough back injury, by using the make function.
You can also use the make function to create a slice with a different length and capacity.
This is needed and you want to use on this one part of a slice.
So when you grow it, it won't allocate a new way if you define its capacity wisely, because here it
creates a slice with the length of three.
Right now I'm going to add another argument to set the returning slices capacity laakso.
When I do saw the make function creates a new slice like this, the return slice is length and covers,
they are not equal.
As you can see, it's like this tree, but its capacity is five.
So this slice can be extended up to five elements afterward.
The one common pattern of using the make function is passing the zero lengths like this so that you
can easily append new elements to the slice.
From its beginning here, I saved this new slice to the variable.
Now I'm going to expand the new element to the slice.
Let's see what happens next.
As you can see, the apparent function adds the new element as the first element, remember the apan
function at Pince after the length of the given slice.
So here the slices length was zero.
So it has a pattern that the new value to the beginning of the slice.
OK, in theory, let's take a look at an example in the ed now.
First of all, let me tell you that you don't need to use the make function.
Yeah, that's right.
We gofers use the make function when we want to optimize our software.
All right, let's say I have received a string slice such as this one, imagine that our database has
only three tasks for now, but it can grow in time.
So this slice has only three elements, but it could have thousands of elements as well.
Anyway, let's say I want to change all the tasks to uppercase.
I want to do it in a new slice.
It is because I don't want to override the original tasks here.
Then I'm going to arrange over the original tasks like so here I'm going to add part to the up tasks.
And now I'm going to use the strings to upper function to make the next task uppercase like this.
OK, let me also bring the up task.
Let's first printed here.
And one more inside the loop here, OK?
That's right.
Well, it works.
However, this court is inefficient.
It allocates three different back injuries each time I packed up tasks less.
Do you know why?
Please post the video and try to guess.
It does so because when there isn't enough capacity in the back, correct, the apparent function creates
a new back injury.
So how can I make this court more efficient?
I mean, how can I prevent array allocations each time?
I can't?
Well, that is where the main function comes into the scene.
You know, using the make function, you can create a slice with a pre allocated Badgingarra with the
given size.
So now I'm going to change the literal here like this.
Here I'm creating a string sliced and I'm going to type land tasks here because I want to create a slice
that is equal to the length of the test.
OK, you'll see why in a second.
Now let's see what it looks like this time.
At first it creates a slice with three elements.
So its length is three, its capacity is also three.
It is because this argument sets both the length and the capacity of the new slice.
Also, pay attention to the content of the elements.
All are zero.
That is because the make function sets the elements to their zero values.
All are ready to use.
However, an interesting thing happens here.
Instead of adding the new elements to the beginning of the slice, the append function a stamp to the
back of the slice.
Do you know why it is possible?
And try to guess.
It does so because, as you know, the aband function upon the elements after the length of a slice
at first here, the slices length was three.
So the next opponent has expanded the new elements as the fourth element, and there wasn't enough space
in the back.
Correct.
So the append function has created a new back injury.
But this is the problem that we want to avoid.
So how can we fix it?
Well, I can simply set the elements by using their index positions like this.
I also need to enable the index variable here.
Cool, now it works, the pointers are the same, of course, if I pants one more element like this,
it will create a new back injury.
IMNSHO.
Now, the pointer has changed, so the opponent has allocated a new era.
OK, let me remove this.
By the way, this court is kind of cumbersome.
It's because I need to keep track of the index of the elements here.
So how can I use the append function instead?
Remember the aband function a pince at the back of a slice.
So if I set the length of the slice to zero, I can use the apan function to do that.
I'm going to type zero here.
Here zero means the length of the slice.
But this third argument means the capacity of the slice.
So this make function will create a slice with zero length.
But with that capacity of three is because the length of the task slice is three.
While there was an error, why this past video and try to guess?
OK, let's take a look at the length of the slice, it is zero, and as I've defined it here, its capacity
is three.
So even though it has three elements in its back injury, it cannot use them yet.
So if I extend this slice like so it can use its full capacity here, I'm using the cap function.
Remember, the cap function returns the capacity of a slice.
So this expression basically equal to the Selous expression without the first index, I'm just slicing
it up to its last element in its back injury.
Anyway, let's try this one.
Cool, now it works, however, this isn't the best cold yet I still cannot use the apan function here.
This card is equal to the previous version of the code.
Let's fix the problems now.
First, I'm going to remove these regionalizing.
Then I'm going to get rid of this index variable and lastly, instead of using the index variable,
I'm going to use the append function like so.
OK, let's try this one.
Now, this court is awesome.
It doesn't allocate a new worry and I don't need to introduce and use the index variable, I'm just
using our old friend aband function.
So how does it work?
Remember, again, the apan function at pant's after the length of a slice.
So at first the length of this slice was zero.
So that pan function has expanded the new element as the first element.
There was enough capacity, so it has used the same array.
Then the length became one.
So that Panth function has expanded the new element as the second element, right after the first element
and so on.
As you can see, you can use the make function, you the length of the new slice.
You can use it to allocate a new baking korei with any length and capacity that you need.
All right, that's all for now.
Thank you for watching.
See you in the next lecture.