IndexOf function for arrays/slices

How to write a functional IndexOf function in golang using generics

IndexOf returns the index of the first occurrence of the specified value in the input slice, or -1 if the slice does not contain the value.

func IndexOf[A comparable](input []A, value A) int {
	for i, v := range input {
		if v == value {
			return i
		}
	}
	return -1
}

and this is a unittest for the function:

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

Technologies: