方法一

优点: 速度最快 缺点: 会导致切片数据顺序改变

1
2
3
4
5
6
7
8
a := []string{"A", "B", "C", "D", "E"}
i := 2

a[i] = a[len(a)-1] // 将数组的最后一位赋值给需要删除的 index 上
a = a[:len(a)-1] // 移除掉最后一个没用的数据

// Output:
// [A B E D]

方法二

优点: 速度会随着切片长度改变 缺点: 保持原有切片顺序

1
2
3
4
5
6
7
a := []string{"A", "B", "C", "D", "E"}
i := 2

a = append(a[:i], a[i+1:]...)

// Output:
// [A B D E]

Benchmark

1
2
3
4
5
6
7
8
9
goos: linux
goarch: amd64
pkg: github.com/yakumioto/go-example/benchmark/delete-element-slice
Benchmark1
Benchmark1-4    1000000000         0.700 ns/op
Benchmark2
Benchmark2-4    93140385            12.9 ns/op
PASS
ok      github.com/yakumioto/go-example/benchmark/delete-element-slice  4.169s

参考

  1. https://yourbasic.org/golang/delete-element-slice
  2. https://blog.golang.org/slices