Welcome back.
OK, let me show you a few examples about the capacity in the coding, Ed. Let me first import my last
package here.
OK, so I'm going to show you the back of the slices.
So I'm going to enable the print bagging option through.
OK, let's start by declaring a nil string slice.
Let me print it.
Pay attention to its pointer, it is zero.
This means that the class has no backing error yet.
That is why its capacity is also zero when Venezuela doesn't have a pointer, its capacity will always
be zero, but not the other way around.
I mean, a slicers capacity can be zero, but it can have a pointer.
As a for example, let me overwrite this slice with an empty slice.
OK, let me bring to the.
This time, it's pointer is not zero, but its capacity is zero, so it has a back injury but it's an
empty area actually go assigns the same dummy error to all the empty slices.
For example, I can create another and the slice on the fly.
As you can see, even though it's a different slice with a different element type, its address is the
same as a previous slice.
This means that for the current go compiler, an empty slice doesn't allocate a new era.
OK, let me assign a non-empty slice to the game's variable with a few retro game titles.
They also printed this time, the slicers length is four, because it sees four elements from its back
injury and its capacity is also four because its ERRA has four elements as well.
This time its pointer is different than an empty slice because this time God creates a real back injury
at this memory location and this slice points to it as an example.
Let me create another slice.
As you can see, it's pointer is different than the previous slice.
This means that God creates different Badgingarra for the slice at a different location.
Now, as an example, I'm going to show you how to use the capacity of a slice in a loop, by the way,
I can do so without touching the game slice.
To do that, I'm going to assign the game slice to a new slice variable like this.
This creates a new slice header only for the part variable, but the game slice and the pot slice have
the same baking array.
OK, let me show you what it looks like.
As you can see, the pointers of these slices are the same.
It's because they both are looking at the same baking array.
To show you my next example, I'm going to make this an attempt to slice like.
So let me print it.
This exploration returns, an empty slice is because the stopping position is zero, however, its capacity
is four.
So there are four elements in its begging array.
Those gray boxes are the elements in its Badgingarra.
This means that I can visualize it again and get access to these elements imagery.
Now I'm going to duplicate this and I'm going to slice it up to its capacity using the cap function.
I could have done the same thing like this because its capacity is far as you can see, I can visualize
it up to its capacity.
Now, let me slice it using a loop.
First, I'm going to loop as long as the parts slices cap is not zero.
Now I'm going to realize the parts lives up to its capacity here.
I override the parts slice using itself because slicing returns a new slice, but it uses the same baking
array.
Right.
You understand how this works in a second.
So the loop will continue realizing the parts lies until its capacity becomes zero.
Let me also print it.
OK, let me show you.
Oops, it goes into an infinite loop.
Can you find the problem, please post a video and try to find the.
The problem is in here, these slices, the parts lies beginning from its first element up to its maximum
capacity, so it always returns a slice with the same elements.
But I need to start from the next element.
So the slice, you'll get smaller and smaller.
As you can see now, it works here on each loop step, the slice gets smaller, its length and capacity
decrease, but its pointer increases until the slice becomes empty.
This happens because here I'm getting the next elements by throwing away the first one and I'm overriding
the same slice.
So its capacity and lengths get smaller and smaller.
By the way, its pointer increases by 16 on each step is because, as you can see, a string value is
16 bytes on a 64 bit machine.
So each element of the slice is further away from one another, exactly 16 bytes, by the way, since
I only overwrite the parts last here.
So the game slice stays the same.
Let me show you both of them first.
OK.
As you can see, the part is empty, but the game is not game slice is still the same because I didn't
touch it.
So this means that you can create different views for the same array by using different slices as I
do here.
This is one of the powers of slices.
If I had only used the game slice instead, I wouldn't be able to access to the begging trace elements
again as an example.
Now I'm going to slice the game slice beginning from its last element plus one to the end.
So this is equal to its length.
As you can see now, the game slice is empty, like the parts slice, so I can no longer access the
elements of its Pechanga.
That's why I used the parts less in parallel to the game at the beginning.
All right.
That's all for now.
If you didn't understand something, please let me know in the Q&A forum.
Thank you for watching.
See you soon.