#269·gods

issue with IndexOf

Author: architagrCreated Nov 17, 2024Updated Aug 18, 2026

sample code

go
func getSampleData() []*user {
	return []*user{
		{
			id:   1,
			name: "test 1",
		},
		{
			id:   2,
			name: "test 2",
		},
		{
			id:   3,
			name: "test 3",
		},
		{
			id:   4,
			name: "test 4",
		},
		{
			id:   5,
			name: "test 5",
		},
	}
}
func TestFind(t *testing.T) {
	userList := arraylist.New(getSampleData())
	index := userList.IndexOf(&user{
		id:   1,
		name: "test 1",
	})
	if index == -1 {
		t.Fatalf("Array list should not return -1 as a response of find")
	}
}

from the above test, we can see that the collection has the value that we are trying to get the index for, this is due to the data being of type pointer stored in this and hence the equal operator will not work.