Welcome back.
Working with slices, you don't have to copy the elements using a loop for that, there is a built in
function called the copy.
It copies the elements of a slice to another slice.
Let's see how it works.
First, you need to type copy that.
You need to pass it.
A destination slice which will receive the elements from the source slice.
So you also need to pass it a source slice like this.
So the copy function will copy the source slices elements to the destination slice.
By the way, the element types of both of the slices should be identical or the copy function won't
work.
All right, let me show you how the copy function works in detail.
Here is a bite size.
Here is another one.
As you can see, the element type of these slices, is it?
OK, let me pass these slices to the copy function so the green slice will be copied to the blue slice
like this.
You might be asking why the copy's only two elements instead of three is because the copy function only
copies the elements based on the length of the smallest slice.
The blue slice has two elements, but the green slice has three elements.
So the slice at the left is the smallest slice.
That is why the copy function copies on two elements here.
The copy function also returns.
How many elements it copies like this so you can know how many elements are copied.
Let's take a look at an example in decoding Ed.
OK, let's say I'm creating a program that prince training probabilities for the next day's, let's
say I have received these training probabilities from a remote API.
Now I'm going to print the data slice.
The data looks good.
OK, now let's calculate the remaining probability.
Here, I'm going to use the person sign twice.
That's because I want to print just a single person sign here so the printer will not interpret it as
a verb.
Now I'm going to calculate the reigning average manually like this.
And lastly, I'm going to divide the total by the length of the slice like this.
Remember, the land function returns and int so I need to convert it to offload six to four like this.
OK, it says that there is a 29 percent chance of raining so we can go out and play the weather is beautiful,
awesome, but not so fast because I received new information, I learned that a hacker has corrupted
the first two elements while I'm receiving the data.
Fortunately, I have received new data for the first two elements.
OK, now I need to copy to the original data, I can do so by manually overriding the data like this.
However, this is cumbersome, right?
And in real life, the data could have been longer.
So let's use a loop instead.
I'm going to loop over the new data slice like this.
Now I'm going to copy the new data from the new data slice to the corrupted data in the data slice like
this.
All right.
Limerence.
It works, but there is nothing magical here, right, instead of a loop.
Let's use the copy function.
Let me remove the loop first here.
I'm going to copy to the data slice from this new slice like this.
Let me also remove this declaration.
Remember, a composite literal, like a slice, literal here creates a new value so I can use it directly
without assigning to a variable.
Let me run it again.
It works cool, the weather will probably be rainy.
All those nasty hackers, anyway, as you can see, the last two elements are the same.
But why?
Well, it's because the cuppy function finds the smallest slice and it copies only that many elements.
So here is Newsless is the smallest one because it has only two elements.
But the data slice here has four elements.
So the cubie function only copies two elements from the new slice through data slice.
Let's see what happens if the original slice is greater than the destination slice.
Now I received five more data points for the new reigning probabilities like these ones.
OK, let's try it.
As you can see, this copying operation completely overwrites the data slice.
That's because the new slice has five elements, but the data slice has four elements.
So the copy function copy is the four elements from the new slice to the data slice.
It also returns how many elements it copies limn, also printed.
As you can see, it prints for it's because the copy function has copied four elements.
So the last data point here is lost.
This means that you cannot create new data elements using the copy function.
It just copies the values from a slice to another one.
You want to grow the slice, you need to use the apan function like this.
Remember, this creates a new value with zero length so I can expand to the beginning of the data.
Now I'm going to pass the new data to here.
As you can see now, the data slice is bigger than before, so it has a new back injury.
Anyway, our focus is the cuppy function here.
So I'm going to commence out the last bands like this.
OK, let's say I want to say the original data.
To do that, I can create a new slice using a make function like this here.
I'm going to pass the length of the data slice to create a new slice with the same length or the original
data slice.
Now I can use the copy function to copy all the elements from the data slice to the new slice like this.
It will copy all the elements because these slices have the same length.
OK, let me also print the saved slice like this.
Cool.
I saved the original data.
Now I can work on the data slice without affecting the safe slice, it is because they have different
backing.
For example, I can change the data slices first element like this.
As you can see, only the data slice sees the change, not the saved slice, by the way, most of the
time I don't use the copy function for cloning a slice because it is cumbersome to write.
First, you need to call the make function that you need to call the copy function instead.
I can clone a slice using the apan function like this here, spanning all the elements of the data slice
to a new slice.
Remember, the append function can only work with slices.
So that is why I'm converting the normal slice to our floor 64 slice here.
Also remember, a new slice doesn't have a back injury, so the apan function creates a new Babchenko
right here.
So the saved slice here, references to that new array.
As you can see, it works as before the safe slice and the data sless have different back injuries.
That's why changing one of them doesn't affect the other one.
These two ways are almost equal, but I prefer the second way because it is more concise.
Cool.
That's all for now.
Thank you for watching.
Seeing the next picture.