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) SParameters & Attributes
| Name | Type | Description |
|---|---|---|
| s | S | Input slice; may share its backing array. |
| i | int | Index or start position. |
| v | ...E | Element 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))}