Skip to content

Commit ac40441

Browse files
TyPh00nCdrgopherbot
authored andcommitted
SliceTricks: use builtin clear
After filtering without allocating, it's recommended to loop over the underlying slice `a` and setting all unneeded values to `nil` manually if they're to be gc'ed. Instead, one might simply use the builtin `clear` on the sub-slice starting at the end of `b`. Change-Id: I25c180bbbccf989ffa0d1fadd54e3a20ff9504c8 GitHub-Last-Rev: ce32262 GitHub-Pull-Request: #24 Reviewed-on: https://go-review.googlesource.com/c/wiki/+/609517 Auto-Submit: Ian Lance Taylor <[email protected]> Commit-Queue: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent e86609b commit ac40441

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)