To reverse a slice in Golang, you can use the built-in "reverse" function from the "sort" package. Here is an example:

Go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package main

import (
	"fmt"
	"sort"
)

func main() {
	// Create a slice of integers
	mySlice := []int{1, 2, 3, 4, 5}

	// Reverse the slice//
	sort.Sort(sort.Reverse(sort.IntSlice(mySlice)))

	// Print the reversed slice
	fmt.Println(mySlice) // Output: [5 4 3 2 1]
}

Alternatively, you can also use the "copy" function to create a new slice that is a reverse of the original slice:

Go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package main

import (
    "fmt"
)

func main() {
    // Create a slice of integers
    mySlice := []int{1, 2, 8, 4, 5}

    // Create a new slice that is a reverse of the original slice
    reversedSlice := make([]int, len(mySlice))
    copy(reversedSlice, mySlice[:])

    // Reverse the new slice in place
    for i, j := 0, len(reversedSlice)-1; i < j; i, j = i+1, j-1 {
        reversedSlice[i], reversedSlice[j] = reversedSlice[j], reversedSlice[i]
    }

    // Print the reversed slice
    fmt.Println(reversedSlice) // Output: [5 4 8 2 1]
}

Both methods will produce the same result, but the first method is more concise and easier to read.

Only reverse

The standard library does not have a built-in function for reversing a slice. Use a for loop to reverse a slice:

Go:
1
2
3
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
    s[i], s[j] = s[j], s[i]
}

Use type parameters to write a generic reverse function in Go 1.18 or later:

Go:
1
2
3
4
5
6
7
8
9
10
11
12
13
func reverse[S ~[]E, E any](s S)  {
    for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
        s[i], s[j] = s[j], s[i]
    }
}

// or

func ReverseSlice[T comparable](s []T) {
    sort.SliceStable(s, func(i, j int) bool {
        return i > j
    })
}

Benchmark:

plaintext:
1
2
3
4
5
goos: darwin
goarch: amd64
cpu: Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
Benchmarkreverse-8    	60623221	        19.79 ns/op	       0 B/op	       0 allocs/op
BenchmarkReverseSlice-8   	  512164	      2294 ns/op	      56 B/op	       2 allocs/op

Ex:

Go:
1
2
3
4
5
6
7
8
9
10
11
func main() {
	// Create a slice of integers
	slice := []int{3, 6, 1, 8, 2, 5}
	// Reverse the slice
	sort.Slice(slice, func(i, j int) bool {
		return slice[i] > slice[j]
	})

	// Print the reversed slice
	fmt.Println(slice) // Output: [8, 6, 5, 3, 2, 1]
}

Use reflect.Swapper to write a function that works with arbitrary slice types in Go version 1.8 or later:

Go:
1
2
3
4
5
6
7
func reverse(s interface{}) {
    n := reflect.ValueOf(s).Len()
    swap := reflect.Swapper(s)
    for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
        swap(i, j)
    }
}

This will return a reversed slice without modifying the original slice.

Algorithm used from official wiki page: https://github.com/golang/go/wiki/SliceTricks#reversing

Go:
1
2
3
4
5
6
7
8
9
10
11
func reverse(s []interface{}) []interface{} {
    a := make([]interface{}, len(s))
    copy(a, s)

    for i := len(a)/2 - 1; i >= 0; i-- {
        opp := len(a) - 1 - i
        a[i], a[opp] = a[opp], a[i]
    }

    return a
}