From 2c8f01e9390fb24e5c3a0d41e98f8d8616497f1b Mon Sep 17 00:00:00 2001 From: Niklas Hennings Date: Fri, 30 Aug 2024 15:01:39 +0200 Subject: [PATCH] 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`. --- SliceTricks.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/SliceTricks.md b/SliceTricks.md index 58f7e8fe..0f77ff2d 100644 --- a/SliceTricks.md +++ b/SliceTricks.md @@ -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