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.
Code
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
cURL to Python & Go ConverterTranslate curl CLI commands with headers, auth, JSON bodies, and timeouts into ready-to-run Python requests and Go net/http client code snippets.Kubernetes Resource CalculatorCalculate per-pod and cluster CPU and memory requests, limits, QoS class, overcommit ratios, headroom, and node fit estimates for Kubernetes workloads.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- What does Slices Delete Zeroes Stale Tail print when run?
- The script runs standalone; its 2 labeled outputs include `after delete:`, `backing array:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.