Skip to content

Commit 45cb09b

Browse files
TyPh00nCdrNiklas Hennings
authored and
Niklas Hennings
committed
SliceTricks: use builtin clear
After filtering without allocating, it is recommended to loop over the underlying slice a and setting all unneeded values to nil manually if they are to be garbage collected. Instead, one might simply use the builtin 'clear' on the sub-slice starting at the end of b.
1 parent 206fae2 commit 45cb09b

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)