Deleting an element from a Go slice

mihalis's picture

This blog post will present two techniques for deleting an element from an existing Go slice.


package main


import "fmt"


func main() {
aSlice := []int{1, 2, 4, 5, 6, 7}
fmt.Println(aSlice)


// Delete element at index i
i := 3
if i > len(aSlice) {
fmt.Println("Cannot delete element", i)
return
}
aSlice = append(aSlice[:i], aSlice[i+1:]...)
fmt.Println(aSlice)


// Delete element at index i
i = 0
if i > len(aSlice) {
fmt.Println("Cannot delete element", i)
return
}
// Replace element at index i with last element
copy(aSlice[i:], aSlice[i+1:])
// Remove last element
aSlice = aSlice[:len(aSlice)-1]
fmt.Println(aSlice)
}


$ go run deleteSlice.go
[1 2 4 5 6 7]
[1 2 4 6 7]
[2 4 6 7]