Reverse function for arrays/slices

How to write a functional Reverse function in golang using generics

Reverse returns a new slice with the elements of the input slice in reverse order.

func Reverse[A any](input []A) []A {
	length := len(input)
	result := make([]A, length)
	for i, v := range input {
		result[length-1-i] = v
	}
	return result
}

and this is a unittest for the function:

func TestReverse(t *testing.T) {
	input := []int{1, 2, 3, 4, 5}
	expected := []int{5, 4, 3, 2, 1}
	result := util.Reverse(input)
	if !reflect.DeepEqual(expected, result) {
		t.Errorf("Expected %v, got %v", expected, result)
	}
}

Technologies: