Skip to content

Instantly share code, notes, and snippets.

@pidb
Created September 23, 2021 01:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pidb/67a93f81624032aaa30b77e54ac7366e to your computer and use it in GitHub Desktop.
Save pidb/67a93f81624032aaa30b77e54ac7366e to your computer and use it in GitHub Desktop.
Better clean slice for go compiler
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