Start learning free

LESSON 111 OF 671 · ARRAYS I

Remove Last Item From Slice in Go

Lesson

Another way to change the data in a slice is by re-slicing it, which removes the last element:

threeArr := []int{1, 4, 6}

// Remove last element:
threeArr = threeArr[:len(threeArr)-1]

// threeArr is now {1, 4}

// Remove last element again:
threeArr = threeArr[:len(threeArr)-1]

// threeArr is now {1}

Assignment
Follow the Coding Tutorial and let's play with some arrays.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check