Files
lo/docs/data/core-splice.md
T
2025-10-06 17:15:49 +02:00

1.4 KiB

name, slug, sourceRef, category, subCategory, playUrl, variantHelpers, similarHelpers, position, signatures
name slug sourceRef category subCategory playUrl variantHelpers similarHelpers position signatures
Splice splice slice.go#L748 core slice https://go.dev/play/p/G5_GhkeSUBA
core#slice#splice
core#slice#slice
core#slice#drop
core#slice#dropright
core#slice#insert
0
func Splice[T any, Slice ~[]T](collection Slice, i int, elements ...T) Slice

Inserts multiple elements at the specified index, with support for negative indices and automatic bounds handling. Negative indices count from the end (-1 means before last element), and indices beyond the slice length append to the end.

// Basic insertion at position 1
result := lo.Splice([]string{"a", "b"}, 1, "1", "2")
// result: []string{"a", "1", "2", "b"}

// Negative index: -1 means before the last element
result = lo.Splice([]string{"a", "b"}, -1, "1", "2")
// result: []string{"a", "1", "2", "b"}

// Index overflow: when index > len(slice), elements are appended
result = lo.Splice([]string{"a", "b"}, 42, "1", "2")
// result: []string{"a", "b", "1", "2"}

// Insert at beginning (index 0)
result = lo.Splice([]int{3, 4, 5}, 0, 1, 2)
// result: []int{1, 2, 3, 4, 5}

// Insert before last element with negative index
result = lo.Splice([]int{1, 2, 3}, -2, 99)
// result: []int{1, 99, 2, 3}

// No elements to insert returns original slice
result = lo.Splice([]string{"a", "b"}, 1)
// result: []string{"a", "b"}