Skip to content

Commit

Permalink
SliceTricks: use builtin clear
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
TyPh00nCdr authored and gopherbot committed Feb 18, 2025
1 parent e86609b commit ac40441
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions SliceTricks.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,7 @@ for _, x := range a {
For elements which must be garbage collected, the following code can be included afterwards:

```go
for i := len(b); i < len(a); i++ {
a[i] = nil // or the zero value of T
}
clear(a[len(b):])
```

### Reversing
Expand Down

0 comments on commit ac40441

Please sign in to comment.