reverse array in go

To reverse an array in go, we can use the standard library function reverse from the sort package. Here's a sample code to reverse an array:

main.go
package main

import (
	"fmt"
	"sort"
)

func main() {
	arr := []int{1, 2, 3, 4, 5}
	fmt.Println("Original array:", arr)

	// Reverse the array
	sort.Sort(sort.Reverse(sort.IntSlice(arr)))

	fmt.Println("Reversed array:", arr)
}
229 chars
17 lines

In the code above, we first define an int array arr with values 1, 2, 3, 4, 5. We then use the sort.Reverse function to reverse the array and print the result.

related categories

gistlibby LogSnag