liveBook · Manning

Generic interface

As explained in Go in Action, Second Edition

A generic interface is an interface parameterized by one or more type parameters whose implementations must match the resulting type-specific contract.

A generic interface defines a contract in terms of a type parameter. In Go, List[T] can specify operations on T without committing callers to one storage structure. Instantiations such as List[int] and List[User] keep those operations type-specific while sharing the same design. 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)
}

List[T any] embeds Collection[T] and adds indexed operations. An implementation must supply the embedded collection methods as well as Get, Set, Insert, and RemoveAt for the same T. section 6.3

A LinkedList[T comparable] provides a concrete implementation using node[T]. Its narrower constraint permits equality checks in Remove and Contains. Add links a new node and updates the size; indexed methods traverse nodes and check bounds. Get returns the zero value of T alongside an error when the index is invalid. Implementing every method completes the contract rather than merely declaring it. section 6.3

Write an operation against the interface

ReverseList[T comparable](l List[T]) uses Size and swaps values obtained with Get through Set. It depends on the list contract, not on linked-list nodes. A LinkedList[int] can therefore be passed to it after construction and ordinary list operations. section 6.3

Keep type arguments aligned when working with an instantiated interface. A Collection[int] holding a *LinkedList[int] can be asserted to that concrete instantiation if its dynamic type matches. An assertion to *LinkedList[string] is rejected by the compiler because that instantiation cannot implement Collection[int]. Separately, when an interface constrains a type parameter, only methods in its method set are available through that parameter. section 6.5.3

How Go in Action, Second Edition covers it

How deep it goes
Works through a linked-list implementation, indexed methods, and a function accepting the interface; also identifies assertion and method-set pitfalls.
Examples
Demonstrates List[T] through LinkedList[T], indexed operations, ReverseList, and an assertion from Collection[int] to *LinkedList[int].
What it assumes
Assumes familiarity with defining generic interfaces and types before developing their concrete implementation.
What you can do after
Build it from scratch: implement a generic list contract and write functions that accept it without depending on its storage structure.
How it connects
Connects generic interfaces to type parameters, constraints, interface embedding, method sets, and type assertions.

Also explained in

Compare how 2 books explain generic interface →

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.