There is a built-in append function that is used to append elements to a slice. We'll see in this lesson that the append function works only with slices, not arrays. There are also some nifty alternate uses of the the append function that make appending super easy.
Checkout the sample code in my github repo.
Instructor: [00:00] To illustrate how we can append to an array or a slice, let's start with this function that I created called create champions that creates five individual champions. Here it is, you can see it returns several champions. First is Akali, Evelynn, Katarina, Elise and Gnar and then returns each of them individually.
[00:25] This isn't a typical way you would write a function, but it works for the example that I'm going to use today. Take a quick look at what a champion is just so you know. Over in the data.go file I have a champion strapped defined. It represented Teamfight Tactics champion, has a name, a slice of classes, a slice of origins and a cost.
[00:52] If we go back over here and look, you can see that I'm initializing all of those values for each champion. Akali for example has an assassin class, an origin of ninja and a gold cost of four. We can see that I've repeated that down through this function.
[01:12] We start by looking at how I add elements to an array. Here I've declared a variable called champs, which is a fixed size array of champions that can contain only three elements. I have to use this method to set elements at a specific index.
[01:31] At index zero, I'm setting Akali, one is Evelynn and two is Katarina. I'm printing out what the array that I've populated looks like. Before I ran this, the compiler's going to complain because I'm not using these two variables that I've declared so I'm going to empty those out. We can see here that my array is three champs, Akali in position zero, Evelynn in one and Katarina in two.
[02:03] Let's try to append something to this array. Let me re-declare Elise and let me try to set Elise at array element three. Let's see what happens. It's actually not going to work because my array is a fixed size of three and I'm trying to add four elements.
[02:27] What this is showing is that you can't actually append beyond the capacity of a fixed element array. You need to declare your arrays to contain enough elements that you can populate everything you need.
[02:43] Let's jump over now and declare a slice of champions. Not an array but a slice. Let me first use the blank variable indicator here because I'm not using Elise anymore. Here I have a slice called assassins and I'm using literal syntax here to declare a slice of champions with no elements because there's nothing in between the curly braces.
[03:12] I'm going to log that out and show you that the length and the capacity is zero. Then I'm going to use the append function to append to assassins three different champions, Akali, Evelynn and Katarina and log that out. Let's see what it looks like here.
[03:31] What you can see here is that the first thing that prints is assassins. Let's enlarge this so we can see the whole thing. The initial log statement prints out that I've created a slice of assassins, it's a length and capacity of zero. Then I used the append function and we can see that it has reallocated the underlying array and made room for three, since I've added three champions to it, Akali, Evelynn and Katarina.
[04:06] Next, I'm going to create and populate another slice. We're going to use Elise and Gnar in this example. Let me declare those and go here and add another little bit of code. I'm declaring a slice of champions called shapeshifters and I'm initializing it using literal syntax to two champions, Elise and Gnar. Those are both shapeshifter champions versus the assassins I created before. I log those out just like I did before.
[04:40] Let's take a look at what we got. Not only did we get the assassins up here. I didn't use append in the second example here, but because I used literal syntax with some elements, I got two shapeshifters, Elise and Gnar.
[04:56] In this example, I'm going to show you an alternate use of append, which is really very handy. In this example up here when I created assassins, you'll notice the first argument to the built in function append is the slice that you want to append to. I specified individual champions as subsequent arguments.
[05:19] What I'm going to do here is create an all champ slice using literal syntax. We start out with a zero value slice and I'm going to use this alternate form of append, the built-in function where I specify the slice that I want to add to, but I'm taking another slice that I declared above. Using this three dots syntax, this tells the compiler to expand all the elements out as if I've listed them individually here.
[05:55] This is the same as me typing Akali, Ev and Kat. Then I do the same thing with my shapeshifters, which is the same thing as me specifying Elise and Gnar, except I don't have to do that. I don't have to have individual elements.
[06:12] I can just say, "Enumerate the contents of this slice and append them all in order to this slice." Append always returns another slice. It maybe the same if it doesn't have to dynamically allocate a new array or it may not.
[06:30] Here, let's logout what we get and let's take a look at this final example and see that all champs has a length of five and actually a capacity of six. We have Akali, Evelynn and Katarina, which was our assassins followed by Elise and Gnar which were our shapeshifters. All of them are in the slice, all champs.