#239·promptui

Fixed bug in New function in list.go

Author: suwakeiCreated Jun 20, 2025Updated Jun 20, 2025

Proposal

When initializing list items, the address of the loop variable item was set to all elements of the slice, so all elements pointed to the value of the last item.

Expected Effect

Each item now points to an independent interface{} value. This allows pointer comparisons in the Index() method, for example, to function correctly.

list/list.go

diff
func New(items interface{}, size int) (*List, error) {
	slice := reflect.ValueOf(items)
	values := make([]*interface{}, slice.Len())

---	for i := range values {
---		item := slice.Index(i).Interface()
---		values[i] = &item
+++	// Each item from the input slice is wrapped in an interface{},
+++	// and a pointer to this interface{} is stored. This ensures that
+++	// each element in `values` points to a unique interface{} holding the item's value.
+++	for i := 0; i < slice.Len(); i++ {
+++		val := slice.Index(i).Interface()
+++		values[i] = new(interface{}) // Allocate new memory for an interface{}
+++		*values[i] = val             // Store the item's value in the new interface{}
	}

	return &List{size: size, items: values, scope: values}, nil
	}
}