Standard library / slices

slices.Insert()

Inserts elements at index i, shifting existing elements. May allocate and panics for i outside [0,len(s)]. Expected stdout: '[1 2 3 4]\n'.

Formal Syntax & Structure

go syntaxgo
func Insert[S ~[]E, E any](s S, i int, v ...E) S

Parameters & Attributes

NameTypeDescription
sSInput slice; may share its backing array.
iintIndex or start position.
v...EElement value or variadic values for this operation.

Return Value

S

Inserts elements at index i, shifting existing elements. May allocate and panics for i outside [0,len(s)].

Working Example

Example Snippetgo
package main
import (  "fmt"  "slices")
func main() {  fmt.Println(slices.Insert([]int{1, 4}, 1, 2, 3))}
Go Reference