How to Delete Elements from a Slice in Go

1. Introduction

Slices in Go are a key data structure, similar to arrays but more flexible and powerful. A common operation needed with slices is the deletion of elements. Unlike some other programming languages, Go does not provide a built-in delete function for slices. Instead, you need to use slice tricks to remove elements. This post will explore how to delete elements from a slice in Go.


Deleting an element from a slice involves re-slicing and possibly using the append function to create a new slice that omits the element(s) you want to remove. Since slices are reference types, modifying the slice content directly affects the underlying array and any other slices that share the same array.

2. Program Steps

1. Define and initialize a slice.

2. Choose an element to delete from the slice.

3. Use slice operations to remove the element and maintain the order.

4. Optionally, remove an element without preserving the order for a more efficient operation.

5. Print the slice before and after deletion to show the change.

3. Code Program

package main

import "fmt"

func main() {
	// Step 1: Define and initialize a slice
	numbers := []int{1, 2, 3, 4, 5}
	fmt.Println("Original slice:", numbers)

	// Step 2: Choose an element to delete, for example the number 3 (index 2)
	indexToRemove := 2

	// Step 3: Remove the element at index 2 and preserve the order
	numbers = append(numbers[:indexToRemove], numbers[indexToRemove+1:]...)
	fmt.Println("Slice after deleting element at index 2:", numbers)

	// Optional Step 4: Remove the element at the last index without preserving order
	// Swap the last element with the one to remove and then slice off the last element
	lastIndex := len(numbers) - 1
	numbers[indexToRemove], numbers[lastIndex] = numbers[lastIndex], numbers[indexToRemove]
	numbers = numbers[:lastIndex]
	fmt.Println("Slice after deleting without preserving order:", numbers)


Original slice: [1, 2, 3, 4, 5]
Slice after deleting element at index 2: [1, 2, 4, 5]
Slice after deleting without preserving order: [1, 2, 5]


1. package main - The package declaration for the Go program.

2. import "fmt" - Imports the Format package for output operations.

3. numbers slice is defined and initialized with a sequence of integers.

4. indexToRemove is defined as 2, which is the index of the element we want to delete from the numbers slice.

5. The append function is used along with slicing operations to remove the element at indexToRemove by concatenating the slice up to indexToRemove and the slice after indexToRemove.

6. The slice after the removal is printed to confirm the deletion of the element.

7. The optional step shows how to remove an element without preserving the order, which can be more efficient, especially for large slices. It swaps the element to remove with the last element and then slices off the last element.

8. The final slice after the optional deletion is printed to show the result of the operation.

9. The output shows the slices at each step, confirming the deletion of the specified elements.