Welcome back!
In this lecture I'm going to show you some examples about the differences between slices and arrays
in the coding editor.
All right let's get started.
Let's say you want to list the books and the computer games you have.
So let's first start with an empty array.
I'm going to declare a books array with five string elements like so. OK let me print it using the v
verb.
All right.
Let me run it. As you can see its length is five and its type is five string array and it contains five empty
string values. All right. Let's add some books into this array.
I'm going to add Dracula
1984 and lastly Island.
Okay.
Let me run it. As you can see it's length is still five.
So it still contains five elements.
Even though I've set only three elements of it. OK, now I'm going to add my favorite computer games
into a slice.
As you can see I didn't declare it's length.
This is exactly what makes it a slice.
It's length is dynamic.
If I had typed the length here, it would be an array not a slice.
Okay.
All right.
Now I'm going to print its type and its elements using the v verb again.
All right.
Let me run it.
I didn't initialize this slice yet so it's zero value is nil.
Let me only print its type so you can better understand it OK.
Let me run it. As you can see, its type is a string slice but unlike an array it doesn't have a length
on its type. Let me show you that I can also use the len function on this slice. Let me run it. As you
can see, I can use the len function even though the slice is nil. By the way,
I can also check whether this slice equals to nil or not.
Let me show. Now it says that the game slice is a nil slice because it hasn't been initialized yet.
All right.
Let's assign some games to the slice.
First I'm going to assign kokémon then I'm gonna assign sims. But it fails. As I told you the slice hasn't
been initialized yet so it doesn't contain any elements.
So I cannot assign any values yet.
OK, let me remove these assignments. Instead I'm going to use a slice literal this time.
Let me change this to a short declaration first.
Okay now I'm going to add two elements: kokémon and sims okay.
As you can see this is just like an array literal, right?
The only difference is that it doesn't have a length on its type so it's a slice literal.
If I had typed a number here, like this, this would be an array literal instead.
As I've said almost everything that you've learned about the arrays in the last section is also available
to slices.
So I'm not going to repeat them here again all right.
Let me run it. As you can see, this time the game slice is not nil.
That's why it prints false here.
Its length is 2 and it contains two elements.
By the way, note that, its type is still a string slice.
So it doesn't matter whether it contains any elements or not, its type never ever changes.
Unlike an array, a slice this length is entirely unrelated of its type.
Wait! I've got new books! So let me declare another array with those books.
Let me compare whether the books array equals to the new books array like so. But I cannot do this,
right?
It's because, the types of these arrays are different. It's because their lengths are different.
In addition to this I cannot assign them to each other.
This is because of the same reason: Their types are different.
Okay let's change the type of this array to five elements like this.
As you can see now it works.
It is because they have the same types now.
Their lengths are the same now.
Let me also print the new books array. Okay. Let me rerun it. As you can see the books array has changed.
Now there are two arrays with the same elements, right?
Remember, an array value contains the entire elements in a single value.
But this is not the case with slices.
OK, now let's try the same thing using the slices this time. I'm going to declare a new slice variable
like this.
I'm going to add some elements to the slice pacman, doom and pong.
Okay let me also print the newGames slice here like this.
Okay let's first try to compare our previous games slice to this newGames slice like this. As you can
see I cannot do this.
This is not because the slices have different lengths. It is because a slice value can only be comparable
to a nil value.
No worries though,
I'll explain why this is like so in the next lectures.
So how can you compare whether a slice is equal to another slice?
One of the idiomatic ways is comparing them using a loop.
I'm going to fast forward this because you already know all about this.
Okay here I've removed the if statement first and I've declared a new string variable as you can see
you can range over a slice just like an array. Here,
I'm checking whether the slices contain the same elements at the same index positions or not.
Okay. Let me run it. As you can see they are not equal.
The games slice contains kokemon and sims; whereas the newGames slice contains pacman, doom and pong.
The interesting thing here is that unlike an array a slice doesn't have a length on this type. So I can
assign slices with different lengths between each other.
Let me show you.
Now I'm going to assign the game slice to the newGames slice like this.
As you can see there isn't any error, right?
Let me run it as you can see now they are equal. It is because now of the newGames and the games are
the same.
Look at their elements.
They are the same.
So it doesn't matter whether their lengths are the same or not.
I can do so because the lengths of a slice isn't a part of its type. Only its element type is a part
of its type. As you can see here, those slices have the same type.
That is why I can assign them.
So their length isn't important.
Of course there are many hidden subtleties here.
Assigning a slice to another slice does some interesting things behind the scenes. But no worries, I'll
explain how it works and what it does
in this section as well. There are a lot of things to explain here.
But it's also good because there are many things to learn! By the way I can also set a slice to a nil value
like this.
When I do so, this slice will lose track of all its elements.
Let me run it. Wow! It says that the slices are equal but they shouldn't be equal.
Oh my God! What's going on here?!
Games slice is nil but the newGames slice is not.
So what is the problem here?
Can you find it? Please pause the video and think about it.
OK.
The problem is this evil range clause.
Let me explain.
Go detects that the slice is nil.
And it doesn't even care to run the range clause here.
So this is completely a nil operation. Actually,
this is a very good thing, it's because you can use a slice in a range clause even if it's nil.
So your code won't crash even in that case.
Remember! "Go loves you"! Well, to solve this problem,
I can simply check whether the game's slice is nil or not like this.
Right?
So if it's nil, I'm going to set the ok variable to "not " as I do so in the loop above.
OK.
Let's try it.
Good.
Now it says that the slices are not equal.
It works. Awesome!
But what would happen if I set the games slice to a slice literal without any elements like this. Can
you guess what the program will print?
Please pause the video now and try to answer.
OK let's see.
It says that the slices are equal.
But why? This is because the games slice is not
nil now, it's empty.
However, there is a subtle difference between a nil slice and an empty slice. An empty slice is an initialized
slice.
So it's not nil.
However, a nil slice is an uninitialized slice.
So it is nil.
Remember, nil means uninitialized.
OK, so, why should I do? OK, here, instead of comparing the games slice to a nil value, I should compare their
lengths instead (the slices').
So I'm going to compare whether the length of the game slice is not equal to the length of the newGames
slice or not like this.
OK.
Let me run it. As you can see now it works.
It says that the slices are not equal.
So in summary, never check whether a slice is empty or not by comparing it to a nil value.
Instead of that check its length. It's because no matter whether a slice is nil or empty, its length
will always be zero.
So you can hit two devils with one stone in a safer way.
All right.
That's all for now.
This was just a glimpse of the slices and it doesn't show you their real powers.
But no worries in the next lectures we'll go deeper into slices.
This was just the beginning.
See you in the next lecture, bye!