Created
September 23, 2021 01:45
-
-
Save pidb/67a93f81624032aaa30b77e54ac7366e to your computer and use it in GitHub Desktop.
Better clean slice for go compiler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
type items []int | |
var nilItems = make(items, 16) | |
func (s *items) truncate1(i int) { | |
*s = (*s)[:i] | |
} | |
func (s *items) truncate2(i int) { | |
var toClear items | |
*s, toClear = (*s)[:i], (*s)[i:] | |
for len(toClear) > 0 { | |
toClear = toClear[copy(toClear, nilItems):] | |
} | |
} | |
func main() { | |
s := items{10, 20, 30} | |
s.truncate2(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment