Skip to content

Commit

Permalink
use builtin clear to clear underlying slice after filtering
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
TyPh00nCdr authored and Niklas Hennings committed Aug 30, 2024
1 parent 206fae2 commit d14e876
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 d14e876

Please sign in to comment.