Welcome.
Now it's time to talk about append function in detail.
All right let's get started.
Let's see how it works.
First you need to type append then you need to pass it a slice value.
And lastly you need to pass it an element. In this most basic form append just append
the elements to the given slice and then it returns a new slice value with the newly appended element
like the len function
The append function is also a built in function. So it can be used everywhere without importing any package.
OK let's take a look at an example. Let's create a new slice using a slice literal like this.
The slice literal is just like an array literal but without a length.
So it creates a slice with three elements right from the start.
So its length becomes 3. Let's append a new element to this slice. After the append the slice stays
the same.
So nothing happens.
Let me show you why. Behind the scenes the append function creates a new slice value.
And it appends the new element to the new slice. It's because the append cannot change the original slice.
So instead it returns a new slice.
That is why the original slice stays the same.
We need to use the new slice instead.
Let's do that.
Here, I'm saving the new slice to the nums slice.
But doing so may look weird to you.
When go into deeper into slices,
I'll talk about why we usually do so later.
Now let's get back to the code. Since I've fixed the code
let's try it one more time. Now it works.
We've got a bigger slice now with four elements.
Okay let's add one more element to the new slice
Now it has five elements so its length becomes five. The append function adds the new elements by looking
at the length of a slice. For example, it appends 4 right after 3 because of the slice's length was 3
It appends 9 right after 4 because the slice's length was 4. By the way you don't need to call
the append function multiple times, there is an easier way. Let me show you. As you can see you can also
append multiple elements like this. I've just separated them using commas. This way you can call the append
only once. You can do so because the append function is a variadic function, so it can accept dynamic number
of arguments. So you can add as many elements as you want.
There is one more syntax by the way: it's called the ellipses; it allows you to append a slice to another slice.
Let me show you. Let's say there is also another slice like this. To append this green slice to the nums slice.
I just need to type it like this. The ellipses operator takes the tens slice here and sends its elements
as arguments to the append function.
So when I run this it appends all the elements of tens slice to the back of the nums slice, and
it returns a larger slice with a length of 5. As you can see this code works as if I'm passing the
arguments manually to the append function like this.
OK let's take a look at an example in the coding editor.
let's say I want to create a todo list and I don't know how many tasks that I'm going to store in that
list.
So I need a data structure that can grow indefinitely.
So it's a good use case for a slice.
Each one of my tasks is a string value so I'm going to declare a new string slice called the todo
like this.
By the way in this section I'm going to use a special library that I've created.
So it will be very easy easier for you to understand what's going on behind the scenes.
However you don't have to use my library.
You can also use a fmt.Println or fmt.Printf. They will all work the same.
OK.
So first of all let's get my library using the go get command like this. Cool!
Let me import it. I'm going to change its name to "s" to keep it short in the code okay.
By the way for the rest of this section when I don't explain it, please always import the pretty slice
package like this.
Okay? My package has a Show function, you can pass it a message and an arbitrary number of slices.
So my function is also a variadic function like the append function.
All right let me show you the todo slice. As you can see the first argument is displayed in here as todo.
, and it just prints nil slice here because a slice doesn't contain any items yet it's a nil slice.
So let's add a new item to the slice here I'm overriding the to do slice variable with the new slice
value.
So behind the scenes the old sliced value disappears.
Let me run it. Now the to do slice contains the new element so its length becomes 1.
It is a larger slice than before.
So always remember the append function returns you a new slice value, so you need to save it over your
original slice or the return slice value will be lost. By the way,
here, todo slice's element type is string, right?
So you can only append string values to this slice.
For example let me try append an integer like this, as you can see doesn't work so, this means that you
can only append the same type of elements to a slice. All right!
Now let me add three more elements. All right!
Let me run it. Cool, now the slice contains 4 elements so its length is 4 now.
I'll explain the cap and ptr here later by the way.
Don't worry about down for now.
My package also shows the index positions of the elements inside the slice.
For example "sing" is at the 0 index and "code" is at the 2 index.
Okay as an example let me get the 3rd element like this.
It just prints code, anyway.
Let me remove this okay.
By the way as I said I can also add multiple items using the append function.
Let me show you. OK. Cool! It's the same output as before, right?
By the way as I showed you before I can also append a slice.
For example let's say I have two todos for tomorrow like this.
Now I'm going to append them to the todo slice like so. OK, let me run it.
As you can see the append function appends the tomorrow slices' elements to the todo slice.
Cool.
As I told you, behind the scenes passing a slice value to the append function almost looks like this. All right.
That's all for now.
See you in the next lecture. Bye bye!