Clear a slice without allocating:
nums := []int{1, 2, 3, 4, 5}
nums = nums[:0]
This sets length to but keeps capacity. The underlying array still exists. New appends reuse it. Use this for slice pools or recycling.
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
Resetting without allocation
Clear a slice without allocating:
nums := []int{1, 2, 3, 4, 5}
nums = nums[:0]
This sets length to but keeps capacity. The underlying array still exists. New appends reuse it. Use this for slice pools or recycling.