Welcome. In this lecture,
I'm going to talk about the basics of slices and without focusing on their unique properties yet. You'll
understand the differences between a slice type and an array type.
All right! Let's get started.
Now you might ask: Why should I care about a slice?
OK, let's take a look at the primary differences between an array and a slice.
Here is an empty array with two elements.
As you know an array is a fixed collection type.
So if you try to add a new element to this array it won't work.
This is because you cannot change its length.
Here is a slice with two elements.
It looks like an array but there is a subtle difference.
So you can add new elements to grow this slice in runtime like this or you can remove elements from it.
So how you can add new elements to a slice in runtime while you cannot do so with an array. Let's take
a look at an array variable. So, why you cannot add new elements to an array or remove the existing elements
from it?
It is because the length of an array is a part of its type, so its length belongs to compile time.
This means that its length cannot change in runtime.
You know, Go is a statically-typed programming language.
So when you declare a type, you cannot change it in runtime afterward.
So its length cannot change in runtime (an array's).
So that's why an array cannot grow or shrink dynamically in runtime.
However, most of the time we need dynamically sized collections and arrays don't pay the bill.
We need something more powerful.
We need slices.
Now let's take a look at a slice.
This is a slice variable here.
The nums variable's type is an int slice.
It looks like an array type, right?
However, there is a subtle difference. As you can see, unlike an array a slice doesn't have a fixed length
on its type.
So a slice's length is not a part of its type.
This is precisely the main reason why a slice can grow and shrink in runtime.
This means that its length belongs to runtime. So its length can change dynamically in runtime.
So you can add new elements to a slice and you can also remove the existing elements from it.
Let's take a look at another difference between a slice and an array.
Here is an array variable.
Remember an array value describes its entire elements.
Right?
So Go sets all the elements of an uninitialized array to zero-values. Here, nums variable's element type
is int. That's why Go sets all its elements to zeros here. As you know, when you call the len function on this
array, it always returns 5. It is because an array's length is fixed. It always have the same number of
elements.
Here is a slice variable. Unlike an array, a slice's zero value is nil.
So this means that an uninitialized slice doesn't have any items. Remember, a nil value doesn't mean
the absence of a value. It means the value hasn't been initialized yet. Here, the nums variable is nil.
So it has a nil value but it still has a type. Its type is an int slice.
So, Go knows that this variable is an int slice and it allows you to use the len function on it.
So that's why you can still get the length of a slice even when it is nil.
So, when you do so, it returns zero because the slice here doesn't have any elements yet. Let's take a look at
the last example.
Let's get the nums slice's first element. But I cannot do so.
It's because it doesn't have any elements yet.
Let's try to get its second element.
This is also meaningless.
Now let's try to get its length again. This time it works.
It is because, as I've said, Go knows that this is a slice.
Let me show you an array and a slice declaration together one more time. As you can see, a slice doesn't
have a length. However an array's length is a part of its type.
So a slice can grow and shrink at runtime.
However an array cannot do so.
That's why most of the time we want to work with slices instead. It's because, as you'll see in this section
they are easy to work with compared to arrays, and lastly, the zero value of a slice is nil whereas
the zero value of an array is a zero value of all of its elements together.
Now let's take a look at the common properties of slices and arrays. A slice or an array can only contain
the same type of elements.
For example here, the nums slice or the nums array can only contain int elements.
As you know from the array section, their element types can be different, of course.
For example, they can also store string values or byte values and so on.
However, all their elements should have the same type. You can access and change the elements of a slice
or an array using the index expressions like so.
And lastly, you can get the length of a slice on an array using the len function. As you can see, you
can work with slices and arrays almost in the same way.
I mean you can use the indexing expressions and the len function on a slice just like you can do so
on an array.
They have the same interface.
All right.
That's all for now. In the next lecture, I'm going to show you an example in the coding editor.
See you there, bye!