WEBVTT 0 00:01.266 --> 00:02.466 Welcome. 1 00:02.466 --> 00:06.136 Now it's time to talk about append function in detail. 2 00:06.143 --> 00:09.800 All right let's get started. 3 00:09.833 --> 00:11.966 Let's see how it works. 4 00:11.966 --> 00:16.176 First you need to type append then you need to pass it a slice value. 5 00:16.176 --> 00:23.163 And lastly you need to pass it an element. In this most basic form append just append 6 00:23.163 --> 00:30.133 the elements to the given slice and then it returns a new slice value with the newly appended element 7 00:31.866 --> 00:32.133 like the len function 8 00:32.140 --> 00:39.166 The append function is also a built in function. So it can be used everywhere without importing any package. 9 00:39.166 --> 00:40.900 The append function is also a built in function. So it can be used everywhere without importing any package. 10 00:40.143 --> 00:48.100 OK let's take a look at an example. Let's create a new slice using a slice literal like this. 11 00:48.100 --> 00:52.143 The slice literal is just like an array literal but without a length. 12 00:53.666 --> 00:57.116 So it creates a slice with three elements right from the start. 13 00:57.130 --> 01:05.133 So its length becomes 3. Let's append a new element to this slice. After the append the slice stays 14 01:05.133 --> 01:06.800 the same. 15 01:06.833 --> 01:08.066 So nothing happens. 16 01:08.133 --> 01:14.150 Let me show you why. Behind the scenes the append function creates a new slice value. 17 01:14.150 --> 01:21.633 And it appends the new element to the new slice. It's because the append cannot change the original slice. 18 01:21.190 --> 01:24.173 So instead it returns a new slice. 19 01:24.176 --> 01:28.966 That is why the original slice stays the same. 20 01:28.110 --> 01:30.190 We need to use the new slice instead. 21 01:30.190 --> 01:33.123 Let's do that. 22 01:33.126 --> 01:36.193 Here, I'm saving the new slice to the nums slice. 23 01:36.193 --> 01:39.333 But doing so may look weird to you. 24 01:39.366 --> 01:41.866 When go into deeper into slices, 25 01:41.866 --> 01:44.140 I'll talk about why we usually do so later. 26 01:44.146 --> 01:48.196 Now let's get back to the code. Since I've fixed the code 27 01:49.033 --> 01:52.143 let's try it one more time. Now it works. 28 01:52.146 --> 01:55.143 We've got a bigger slice now with four elements. 29 01:55.170 --> 01:58.136 Okay let's add one more element to the new slice 30 02:01.100 --> 02:08.266 Now it has five elements so its length becomes five. The append function adds the new elements by looking 31 02:08.266 --> 02:15.300 at the length of a slice. For example, it appends 4 right after 3 because of the slice's length was 3 32 02:15.466 --> 02:22.146 It appends 9 right after 4 because the slice's length was 4. By the way you don't need to call 33 02:22.153 --> 02:31.233 the append function multiple times, there is an easier way. Let me show you. As you can see you can also 34 02:31.433 --> 02:38.176 append multiple elements like this. I've just separated them using commas. This way you can call the append 35 02:38.196 --> 02:46.900 only once. You can do so because the append function is a variadic function, so it can accept dynamic number 36 02:46.900 --> 02:50.666 of arguments. So you can add as many elements as you want. 37 02:50.666 --> 02:57.170 There is one more syntax by the way: it's called the ellipses; it allows you to append a slice to another slice. 38 02:57.170 --> 03:05.130 Let me show you. Let's say there is also another slice like this. To append this green slice to the nums slice. 39 03:06.126 --> 03:14.120 I just need to type it like this. The ellipses operator takes the tens slice here and sends its elements 40 03:14.136 --> 03:16.193 as arguments to the append function. 41 03:17.066 --> 03:23.100 So when I run this it appends all the elements of tens slice to the back of the nums slice, and 42 03:23.103 --> 03:30.033 it returns a larger slice with a length of 5. As you can see this code works as if I'm passing the 43 03:30.033 --> 03:33.400 arguments manually to the append function like this. 44 03:33.160 --> 03:36.106 OK let's take a look at an example in the coding editor. 45 03:39.120 --> 03:45.180 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 46 03:45.180 --> 03:46.116 list. 47 03:46.116 --> 03:50.033 So I need a data structure that can grow indefinitely. 48 03:50.133 --> 03:53.133 So it's a good use case for a slice. 49 03:53.133 --> 03:59.333 Each one of my tasks is a string value so I'm going to declare a new string slice called the todo 50 03:59.366 --> 04:00.900 like this. 51 04:00.163 --> 04:05.633 By the way in this section I'm going to use a special library that I've created. 52 04:05.633 --> 04:10.166 So it will be very easy easier for you to understand what's going on behind the scenes. 53 04:10.633 --> 04:12.173 However you don't have to use my library. 54 04:12.193 --> 04:18.126 You can also use a fmt.Println or fmt.Printf. They will all work the same. 55 04:18.143 --> 04:19.600 OK. 56 04:19.600 --> 04:25.733 So first of all let's get my library using the go get command like this. Cool! 57 04:26.116 --> 04:33.110 Let me import it. I'm going to change its name to "s" to keep it short in the code okay. 58 04:33.140 --> 04:39.123 By the way for the rest of this section when I don't explain it, please always import the pretty slice 59 04:39.123 --> 04:40.136 package like this. 60 04:40.180 --> 04:49.066 Okay? My package has a Show function, you can pass it a message and an arbitrary number of slices. 61 04:49.166 --> 04:54.766 So my function is also a variadic function like the append function. 62 04:54.106 --> 05:01.120 All right let me show you the todo slice. As you can see the first argument is displayed in here as todo. 63 05:01.120 --> 05:09.333 , and it just prints nil slice here because a slice doesn't contain any items yet it's a nil slice. 64 05:09.333 --> 05:15.103 So let's add a new item to the slice here I'm overriding the to do slice variable with the new slice 65 05:15.103 --> 05:16.866 value. 66 05:16.113 --> 05:20.666 So behind the scenes the old sliced value disappears. 67 05:20.666 --> 05:27.103 Let me run it. Now the to do slice contains the new element so its length becomes 1. 68 05:27.116 --> 05:29.166 It is a larger slice than before. 69 05:30.233 --> 05:36.106 So always remember the append function returns you a new slice value, so you need to save it over your 70 05:36.106 --> 05:41.163 original slice or the return slice value will be lost. By the way, 71 05:41.163 --> 05:45.133 here, todo slice's element type is string, right? 72 05:45.136 --> 05:49.366 So you can only append string values to this slice. 73 05:49.866 --> 05:56.143 For example let me try append an integer like this, as you can see doesn't work so, this means that you 74 05:56.143 --> 06:01.153 can only append the same type of elements to a slice. All right! 75 06:01.153 --> 06:06.173 Now let me add three more elements. All right! 76 06:06.183 --> 06:14.633 Let me run it. Cool, now the slice contains 4 elements so its length is 4 now. 77 06:14.666 --> 06:18.466 I'll explain the cap and ptr here later by the way. 78 06:18.633 --> 06:19.183 Don't worry about down for now. 79 06:20.166 --> 06:25.000 My package also shows the index positions of the elements inside the slice. 80 06:25.133 --> 06:29.150 For example "sing" is at the 0 index and "code" is at the 2 index. 81 06:29.186 --> 06:33.126 Okay as an example let me get the 3rd element like this. 82 06:34.163 --> 06:36.193 It just prints code, anyway. 83 06:36.190 --> 06:38.176 Let me remove this okay. 84 06:39.366 --> 06:44.633 By the way as I said I can also add multiple items using the append function. 85 06:44.700 --> 06:52.123 Let me show you. OK. Cool! It's the same output as before, right? 86 06:52.176 --> 06:57.866 By the way as I showed you before I can also append a slice. 87 06:57.106 --> 07:02.103 For example let's say I have two todos for tomorrow like this. 88 07:02.133 --> 07:08.500 Now I'm going to append them to the todo slice like so. OK, let me run it. 89 07:08.113 --> 07:14.566 As you can see the append function appends the tomorrow slices' elements to the todo slice. 90 07:14.766 --> 07:15.133 Cool. 91 07:15.333 --> 07:22.176 As I told you, behind the scenes passing a slice value to the append function almost looks like this. All right. 92 07:22.176 --> 07:23.170 That's all for now. 93 07:23.183 --> 07:25.633 See you in the next lecture. Bye bye!