Implement a function that reverses a slice of integers

Technology CommunityCategory: GolangImplement a function that reverses a slice of integers
VietMX Staff asked 3 years ago
func reverse(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() {
	a := []int{1, 2, 3}
	reverse(a)
	fmt.Println(a)
	// Output: [3 2 1]
}