Remove all elements Keep allocated memory Empty slice vs. nil slice Remove all elements To remove all elements, simply set the slice to nil. a := []string{"A", "B", "C", "D", "E"} a = nil fmt.Println(a, len(a), cap(a)) // [] 0 0 This will release the underlying array to the garbage collector (assuming there are no other references). Keep allocated memory To keep the underlying array, slice the sli
![How to best clear a slice: empty vs. nil](https://cdn-ak-scissors.b.st-hatena.com/image/square/daa5f4958ce05083d9ebab54856884163c1800e4/height=288;version=1;width=512/https%3A%2F%2Fyourbasic.org%2Fgolang%2Fwindow-cleaner.jpg)