sliceutil.go 573 B

1234567891011121314151617
  1. // Copyright (C) 2023 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package sliceutil
  7. // RemoveAndZero removes the element at index i from slice s and returns the
  8. // resulting slice. The slice ordering is preserved; the last slice element
  9. // is zeroed before shrinking.
  10. func RemoveAndZero[E any, S ~[]E](s S, i int) S {
  11. copy(s[i:], s[i+1:])
  12. s[len(s)-1] = *new(E)
  13. return s[:len(s)-1]
  14. }