Skip to content

Commit 1484897

Browse files
authored
use builtin clear to clear underlying slice after filtering
Instead of looping over the underlying slice `a` and setting all unneeded values to `nil` manually, one might simply use the builtin `clear` on the sub-slice starting at the end of `b`.
1 parent 206fae2 commit 1484897

File tree

1 file changed

+1
-3
lines changed

1 file changed

+1
-3
lines changed

SliceTricks.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,7 @@ for _, x := range a {
187187
For elements which must be garbage collected, the following code can be included afterwards:
188188

189189
```go
190-
for i := len(b); i < len(a); i++ {
191-
a[i] = nil // or the zero value of T
192-
}
190+
clear(a[len(b):])
193191
```
194192

195193
### Reversing

0 commit comments

Comments
 (0)