Go 1.21 added a clear function that takes in a slice and sets all of the slice’s elements to their zero value. The length of the slice remains unchanged. The following code:
s := []string{"first", "second", "third"}
fmt.Println(s, len(s))
clear(s)
fmt.Println(s, len(s))
prints out:
[first second third] 3
[ ] 3
(Remember, the zero value for a string is an empty string ""!)
Progress is saved on this device.