Forked from rv64m/better-clean-slice-for-go-compiler.go
Created
September 23, 2021 02:13
-
-
Save jrkeen/06ce2d90fc9cb00881c84622139a64d1 to your computer and use it in GitHub Desktop.
Better clean slice for go compiler
This file contains hidden or 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