0
0
GoProgramBeginner · 2 min read

Go Program to Reverse Slice with Example and Explanation

In Go, you can reverse a slice in-place by swapping elements from the start and end using a loop like for i, j := 0, len(slice)-1; i < j; i, j = i+1, j-1 { slice[i], slice[j] = slice[j], slice[i] }.
📋

Examples

Input[1, 2, 3, 4, 5]
Output[5 4 3 2 1]
Input[10, 20, 30]
Output[30 20 10]
Input[]
Output[]
🧠

How to Think About It

To reverse a slice, think of it like flipping a row of books: swap the first with the last, the second with the second last, and so on until you reach the middle. Use two indexes, one starting at the beginning and one at the end, and move them toward each other while swapping elements.
📐

Algorithm

1
Start with two indexes: one at the beginning (i) and one at the end (j) of the slice.
2
While i is less than j, swap the elements at positions i and j.
3
Move i forward by one and j backward by one.
4
Repeat until i is no longer less than j.
5
Return the reversed slice.
💻

Code

go
package main

import "fmt"

func reverseSlice(s []int) {
    for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
        s[i], s[j] = s[j], s[i]
    }
}

func main() {
    slice := []int{1, 2, 3, 4, 5}
    reverseSlice(slice)
    fmt.Println(slice)
}
Output
[5 4 3 2 1]
🔍

Dry Run

Let's trace reversing the slice [1, 2, 3, 4, 5] through the code

1

Initial indexes and slice

i=0, j=4, slice=[1 2 3 4 5]

2

Swap elements at i and j

Swap slice[0] and slice[4]: slice becomes [5 2 3 4 1]

3

Move indexes

i=1, j=3

4

Swap elements at i and j

Swap slice[1] and slice[3]: slice becomes [5 4 3 2 1]

5

Move indexes

i=2, j=2 (stop because i is not less than j)

ijSlice state
04[1 2 3 4 5]
13[5 2 3 4 1]
22[5 4 3 2 1]
💡

Why This Works

Step 1: Two pointers approach

We use two pointers, i at the start and j at the end, to swap elements moving inward.

Step 2: Swapping elements

Swapping s[i] and s[j] reverses their positions without needing extra space.

Step 3: Stopping condition

When i meets or passes j, the slice is fully reversed.

🔄

Alternative Approaches

Create a new reversed slice
go
package main

import "fmt"

func reverseNewSlice(s []int) []int {
    n := len(s)
    result := make([]int, n)
    for i := 0; i < n; i++ {
        result[i] = s[n-1-i]
    }
    return result
}

func main() {
    slice := []int{1, 2, 3, 4, 5}
    reversed := reverseNewSlice(slice)
    fmt.Println(reversed)
}
This method uses extra memory to create a new reversed slice instead of reversing in-place.
Using recursion
go
package main

import "fmt"

func reverseRecursive(s []int, start, end int) {
    if start >= end {
        return
    }
    s[start], s[end] = s[end], s[start]
    reverseRecursive(s, start+1, end-1)
}

func main() {
    slice := []int{1, 2, 3, 4, 5}
    reverseRecursive(slice, 0, len(slice)-1)
    fmt.Println(slice)
}
This method uses recursion to swap elements but may be less efficient for large slices due to call stack overhead.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs once for half the slice length, so it is O(n) where n is the slice length.

Space Complexity

The reversal is done in-place with no extra slice, so space complexity is O(1).

Which Approach is Fastest?

In-place swapping is fastest and uses least memory; creating a new slice uses extra space and recursion adds call overhead.

ApproachTimeSpaceBest For
In-place swapO(n)O(1)Memory efficient and fast
New reversed sliceO(n)O(n)When original slice must not be changed
RecursionO(n)O(n)Conceptual clarity but less efficient for large slices
💡
Use two indexes from start and end to swap elements until they meet for an efficient in-place reverse.
⚠️
Trying to reverse by appending elements to a new slice without preserving order or forgetting to swap elements properly.