slice remove element code example
Example 1: go delete from slice
func remove(slice []int, s int) []int {
return append(slice[:s], slice[s+1:]...)
}
Example 2: how to remove element from backing array slice golang
package main
import (
"fmt"
)
func RemoveIndex(s []int, index int) []int {
ret := make([]int, 0)
ret = append(ret, s[:index]...)
return append(ret, s[index+1:]...)
}
func main() {
all := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Println("all: ", all)
removeIndex := RemoveIndex(all, 5)
fmt.Println("all: ", all)
fmt.Println("removeIndex: ", removeIndex)
removeIndex[0] = 999
fmt.Println("all: ", all)
fmt.Println("removeIndex: ", removeIndex)
}