liveBook · Manning

List

As explained in Go in Action, Second Edition

A list is an ordered collection of elements that supports sequential access and typically dynamic size.

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.

How Go in Action, Second Edition covers it

How deep it goes
Works through a linked-list implementation, including node linking, bounds checks, equality searches, indexed access, and reversal.
Examples
Demonstrates LinkedList[int] operations and reverses a list through a generic List interface.
What it assumes
Assumes Go generics, type parameters, constraints, and an earlier node type while showing how those pieces implement a collection.
What you can do after
Build it from scratch as a type-safe linked list and apply reusable functions such as reversal through its generic interface.
How it connects
Connects lists with generics, type parameters, interfaces, linked lists, comparable constraints, and collection algorithms.

Also explained in

Compare how 21 books explain list →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Action, Second Edition. The section links point to the parts of the book it draws on.