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`.
  • Loading branch information
TyPh00nCdr authored and Niklas Hennings committed Aug 31, 2024
1 parent 206fae2 commit 2c8f01e
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 2c8f01e

Please sign in to comment.