Golang slices.Insert
last modified April 20, 2025
This tutorial explains how to use the slices.Insert
function in Go.
We'll cover slice insertion operations with practical examples.
The slices.Insert function inserts elements into a slice at a specified index. It's part of Go's experimental slices package.
This function is useful for modifying slices by adding new elements at any position. It returns a new slice with the inserted elements.
Basic slices.Insert Example
The simplest use of slices.Insert
adds one element at position 2.
The original slice remains unchanged.
package main import ( "fmt" "slices" ) func main() { numbers := []int{1, 2, 3, 4, 5} newNumbers := slices.Insert(numbers, 2, 99) fmt.Println("Original:", numbers) fmt.Println("Modified:", newNumbers) }
We create a slice of numbers and insert 99 at index 2. The new slice contains the inserted element while the original stays intact.
Inserting Multiple Elements
slices.Insert
can insert multiple elements at once. This example
adds three strings at position 1.
package main import ( "fmt" "slices" ) func main() { fruits := []string{"apple", "banana", "cherry"} newFruits := slices.Insert(fruits, 1, "orange", "pear", "grape") fmt.Println("Original:", fruits) fmt.Println("Modified:", newFruits) }
The function accepts variadic arguments for elements to insert. All elements are added consecutively starting at the specified index.
Inserting at the Beginning
To insert at the start of a slice, use index 0. This example prepends elements.
package main import ( "fmt" "slices" ) func main() { letters := []string{"d", "e", "f"} newLetters := slices.Insert(letters, 0, "a", "b", "c") fmt.Println("Original:", letters) fmt.Println("Modified:", newLetters) }
Inserting at index 0 effectively prepends elements to the slice. The original slice remains unchanged.
Inserting at the End
To append elements, use the slice length as the index. This example adds elements to the end.
package main import ( "fmt" "slices" ) func main() { colors := []string{"red", "green", "blue"} newColors := slices.Insert(colors, len(colors), "yellow", "purple") fmt.Println("Original:", colors) fmt.Println("Modified:", newColors) }
Using len(slice)
as the index appends elements. This is equivalent
to using the built-in append
function.
Inserting into an Empty Slice
slices.Insert
works with empty slices. This example demonstrates
inserting into a nil slice.
package main import ( "fmt" "slices" ) func main() { var empty []int newSlice := slices.Insert(empty, 0, 10, 20, 30) fmt.Println("Original:", empty) fmt.Println("Modified:", newSlice) }
Inserting into an empty slice creates a new slice with the specified elements. The operation is valid even with index 0 on a nil slice.
Inserting with Out-of-Bounds Index
Attempting to insert beyond the slice length causes a panic. This example shows the safe way to handle insertions.
package main import ( "fmt" "slices" ) func main() { data := []int{1, 2, 3} // Safe insertion if len(data) >= 5 { newData := slices.Insert(data, 5, 99) fmt.Println(newData) } else { fmt.Println("Cannot insert beyond slice length") } // Correct approach newData := slices.Insert(data, len(data), 99) fmt.Println("Safe insertion:", newData) }
Always check slice bounds before inserting. The function panics if the index is greater than the slice length.
Practical Example: Inserting into a Sorted Slice
This practical example maintains a sorted slice by inserting at the correct position. It finds the insertion point first.
package main import ( "fmt" "slices" ) func main() { sorted := []int{10, 20, 30, 40, 50} newValue := 25 // Find insertion point i, _ := slices.BinarySearch(sorted, newValue) // Insert at the correct position newSorted := slices.Insert(sorted, i, newValue) fmt.Println("Original:", sorted) fmt.Println("Modified:", newSorted) }
We use slices.BinarySearch
to find where to insert the new value.
This maintains the sorted order efficiently.
Source
Go experimental slices package documentation
This tutorial covered the slices.Insert
function in Go with practical
examples of inserting elements into slices at various positions.
Author
List all Go tutorials.