A list is an ordered collection whose interface exposes positional access alongside collection operations. In Go, a generic List[T] can work with values such as int or User while keeping operations type-safe and avoiding duplicated implementations. section 6.3
Listing 6.12 Defining a generic List interface
type List[T any] interface {
Collection[T]
Get(index int) (T, error)
Set(index int, item T) error
Insert(index int, item T) error
RemoveAt(index int) (T, error)
}
The interface defines the list contract: Get, Set, Insert, and RemoveAt use indexes, while Collection[T] supplies the shared collection behavior. section 6.3
A LinkedList[T comparable] can implement List[T any]. Its nodes store values of the type parameter and link to the next node. The comparable constraint enables equality-based operations such as Remove and Contains, while remaining compatible with the broader interface constraint. section 6.3
Using a typed linked list
Create LinkedList[int], add 10, 20, and 30, insert 15 at index 1, retrieve an item with Get, replace it with Set, and remove an item with RemoveAt. The same list abstraction can be used with other supported element types.
Reusable list algorithm
ReverseList[T comparable] accepts the List[T] interface rather than a concrete linked list. It swaps elements from opposite ends through Get and Set, demonstrating how generic algorithms can operate across list implementations.




















