Slices Delete Zeroes Stale Tail

Watch slices.Delete shift elements left and zero the stale tail since Go 1.22, so old references into the backing array cannot leak data.

GoData Integrity

Code

gosnippet
19 lines
package main

import (
	"fmt"
	"slices"
)

func main() {
	items := []int{1, 2, 3, 4, 5, 6}

	// 删除 [1,3):b 区间元素移除,后续元素左移补位
	items = slices.Delete(items, 1, 3)
	fmt.Println("after delete:", items, "len:", len(items), "cap:", cap(items))

	// Go 1.22+ 的 slices.Delete 会把过期的尾部元素清零(zeroes the stale tail)
	// 通过 cap 重新切片观察底层数组:残留槽位是零值,不会泄漏旧数据
	backing := items[:cap(items)]
	fmt.Println("backing array:", backing)
}

A closer look

Under the hood: the function main.

Points of care

  • Nothing but built-in APIs — the supply chain question does not arise.

Using it

The declaration to build on: func main()

Output lines are prefixed (after delete:, backing array:), so each printed line names the step that wrote it.

Key statements

items = slices.Delete(items, 1, 3)

Interactive Tools for This Topic

FAQ

What does Slices Delete Zeroes Stale Tail print when run?
The script runs standalone; its 2 labeled outputs include `after delete:`, `backing array:`.