Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Remove Elements from a slice in Go

    Jeff RobertsJeff Roberts
    goGo

    Go has a special indexing syntax that forms the basis of removing one or more elements from a slice. I will teach you this syntax and show you its various forms.

    Based upon that, I'll show you how to remove a single element from a slice. However, you may find this somewhat limiting. I also will show you a more typical example of removing elements from a slice as we iterate over it. This technique shows you how to remove multiple elements from a slice.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: How do we remove from a slice? Well, let's start with some champions that I've loaded from a file. Let's take a look at what this looks like. This is just a JSON file with a bunch of champions in it from a game I like to play called Teamfight Tactics. Each champion looks like this.

    If I look at the data.go file, each champion has its a strap that has a name, it has some classes, some origins and a gold cost. Let's go ahead and run this and we'll see that I've loaded up a whole bunch of champions. The slice says 56 elements and you can see all of the champions that I've loaded from the file.

    Removing elements from a slice uses a syntax that's similar to the indexing syntax that you're probably used to when you work with arrays or slices. I've loaded all these champions into a variable called all, which is a slice. I'm going to use here the index syntax to grab the very first champion out of this slice, and in the next line, at line 17, I'm going to print that to the console.

    Let's ran this and we can see when I expand this, the first champion is Akali. It's actually the first element in the slice. In Go, I can use a different type of indexing to grab a chunk of the slice by specifying using this syntax the from index and the to index.

    Now, when I specify this, the from index is inclusive. Meaning, it will include the element as that element in the first index, but it will exclude the last. It will include everything up to element 20, but not including element 20. Then I'm going to print this out. Let's see what happens here.

    If we take a look, it says the first 20 champions are and it lists champions from 0through 19, since slices are zero-based. Go has some shortcuts. I'm just going to overwrite this same variable that I already declared just so that I don't have to comment anything out. I can leave out the first element, the first index and it will assume zero.

    If I ran this again, we can see that it has the same effect of specifying both. It still grab the first 20 champions and it's the same list that I had when I specified both indexes. These two lines are actually equivalent. Guess what, if I can omit the first, then I can omit the last.

    I'm going to re-declare this or reassign the same variable and I'm going to specify 10 for the first index and leave out the last index. What that means is that it will take everything up through the end of the slice.

    Let's ran this and it should take 10 though the last element in the slice. This time, it takes 46 champions. You guessed it, if I can omit the first and I can omit the last, then I can omit them all. I'm not going to specify any indexes here. The first one defaults to zero and the last one defaults to the end.

    When I ran this, I'm going to get the entire array, 56 champions which are the same number that I loaded from the file. Why did I show you this? Because removing elements from a slice is based upon this syntax that I just showed you right up here.

    Here, I'm going to show you how to remove one element from a slice. I have a new variable called most and I'm using the built in append function and I'm using this indexing syntax to take elements zero through nine.

    Remember, the last element is excluded and append takes the slice that I want to add to or append to and then a list of one or more elements that I'm going to add to it. I'm going to use the same syntax to take elements 11 through the end.

    I'm using the three dots syntax which tells the compiler to enumerate all of those as if I've typed them all out. Then I'm going to log out. How many champions are in the most variable and then how many champions are in the all variable which should be unaltered.

    Let's ran this and we can see when I expand this, if I scroll back a little, there are 55 champions in the most variable. I omitted the champion at element 10, which should be Fiora which is right here, right after Draven. If I look down here and I find Draven right here, there is no Fiora.

    Fiora is gone because I removed just that one element. You can see the all slice is unaltered, it still has 56 elements. I used this syntax that I taught you above to extract or remove one element from the slice and append to create a new slice which I assigned to the variable most.

    This technique that I taught you up here is not that commonly used because it requires you to know the specific index or continuous indexes of the elements that you want to remove. What I'm going to show you now is a more common usage where you might be iterating over a slice and you want to dynamically determine if you want to remove something.

    This little block of code here is going to iterate using range over all the champions and I'm ignoring the index variable, but I care about the champ. I initialized this index counter before the loop to zero and I'm going to omit all sorcerers from the all slice.

    If the champion that I'm currently iterating over has a class of sorcerer, then I'm just going to continue which skips the rest of this loop. If it's not a sorcerer, then I'm going to change the all slice in place by setting at the index zero in the first case. I'm initializing or setting that to the champ that I'm on and then I'm incrementing the index.

    If you look at this code closely, it might be a little confusing, I am altering the all slice in place by keeping a separate counter and omitting sorcerers. Outside of the loop, I'm using the syntax that I taught you to create a new slice from zero up to and excluding the last index.

    I'll print the results of that out and what we will see here is that I have 47 champions. If you look closely at this, none of them are sorcerers because they got removed dynamically using this little bit of code that I wrote right here.