liveBook · Manning

Chapter 6: Generics

What you will learn in chapter 6 of Go in Action, Second Edition

Introduces Go generics as a type-safe layer of abstraction and previews the chapter’s progression from type parameters to reusable collections and standard-library utilities.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces Go generics as a type-safe layer of abstraction and previews the chapter’s progression from type parameters to reusable collections and standard-library utilities.

Key terms: generics, type parameters, compile-time type safety, generic collection types, utility functions, lists, sets, maps, standard library

6.1 Typed parameters in generics

This section introduces Go type parameters through a reusable filter and generic collections, showing how constraints preserve compile-time type safety.

Key terms: Filter, generics, interface{}, type assertions, compile-time safety, type parameter, T, any, constraint, type inference

6.1.1 Using interface constraints

This subsection uses a Stringer-constrained linked list to show how an interface constraint restricts generic types at compile time.

Key terms: StringLinkedList, fmt.Stringer, interface constraint, compile-time error, String()

6.1.2 Defining custom constraints

This section shows how custom interfaces can restrict generic code to selected types and explains why an exact type list excludes newly defined types with the same underlying type.

Key terms: custom interfaces, Numeric interface, constraint, underlying type

6.1.3 Using underlying types with tilde (~)

This section shows how tilde-based type sets let generic constraints accept custom types by underlying type, and how constraints can be combined into broader ones.

Key terms: tilde (~) operator, type sets, underlying type, Numeric, MyInt, Ordered, constraint hierarchy

6.1.4 Summary of type constraints

This subsection recaps how constraints control permitted types and shows how a simple generic Filter function works with recipe types.

Key terms: built-in interfaces, custom type sets, tilde operator, type constraints, Filter, any, Recipe, type inference, T

6.2 Implementing generic functions

This section shows how generic functions create and operate on typed collections, including transforming elements from one type to another.

Key terms: NewLinkedList, LinkedList[T], T comparable, PrintCollection, Collection[T], List[T], type assertion, RecipeBox, Recipe, string

6.3 Implementing generic interfaces

The section implements a generic list interface with a linked list, then uses that interface to write a type-safe list-reversal function.

Key terms: List[T], LinkedList[T], type parameter, interface, any, comparable, node[T any], Get, Set, Insert

6.4 Multiple type parameters

This section uses a generic map to show how independent type parameters relate key and value types in reusable, type-safe abstractions.

Key terms: type parameters, K comparable, V any, constraints, Map[K, V], HashMap, Put, Get, Remove, ContainsKey

6.5 Limitations and considerations

This introduction frames Go generics as a tradeoff between reusable abstractions and clear, concrete code, then previews the considerations that guide their use.

Key terms: generics, concrete, type-specific code, abstraction, code reuse, simplicity, clarity, performance, language restrictions, common pitfalls, backward compatibility

6.5.1 Performance considerations

This section explains how Go compiles generic functions and how to assess and avoid performance costs in generic code.

Key terms: GC-shape stenciling, partial monomorphization, GC shape, stencil, internal dictionary, generic sum function, SumInt, inlining, interface overhead, heap

6.5.2 Limitations of Go generics

This section identifies two limits of Go generics: type parameters cannot define constants or struct field names, and operators require constraints that permit them.

Key terms: type parameters, constants, struct field names, operators, type parameter, constraint, compile-time error, type safety

6.5.3 Common gotchas

This section identifies common Go generics pitfalls and explains how to recognize or avoid them.

Key terms: interface constraint, method set, fmt.Stringer, type assertion, generic interfaces, type parameters, Collection[int], *LinkedList[int], *LinkedList[string], type parameter shadowing

6.5.4 When to use generics

This section gives practical criteria for deciding when Go generics justify their added complexity and when simpler code is clearer.

Key terms: generics, utility function, compile-time guarantees, type relationships, plain interface, concrete type, type arguments, independent call sites

6.5.5 Backward compatibility

This section explains that Go generics preserve existing code and can be adopted gradually.

Key terms: Go 1 compatibility promise, backward compatibility, interoperable, type parameters

6.6 Standard library support

Introduces standard-library support for generics and previews the packages that provide reusable, type-safe utilities.

Key terms: standard library, generics, constraints, slices, maps, cmp

6.6.1 The constraints package

This section explains which ordering constraint to use in generic Go code and how the choice depends on the Go version.

Key terms: cmp.Ordered, type constraint, golang.org/x/exp/constraints, constraints.Ordered, Go 1.21

6.6.2 The slices and maps packages

Introduces the standard library’s generic slices and maps packages and shows how they support type-safe operations on ordinary collections and domain types.

Key terms: slices, maps, slices.Sort, maps.Equal, type parameters, slices.SortFunc, RecipeBox, serving size, cmp.Ordered, maps.Keys

6.6.3 The cmp package

Introduces cmp.Compare as a standard-library way to compare ordered values in generic Go code.

Key terms: cmp package, cmp.Compare, ordering

6.7 Just getting started

This section recaps how Go generics support reusable, type-safe code while emphasizing their tradeoffs and Go’s preference for simplicity.

Key terms: generics, type parameters, constraints, generic collections, compile time, performance tradeoffs, language restrictions, common pitfalls, simplicity, clarity

6.8 Summary

This summary recaps how Go generics provide type-safe reuse through constrained type parameters, generic data structures, and standard-library support, while noting tradeoffs.

Where this is used later

Concepts explained in this chapter

← Chapter 5: Working with types Chapter 7: Errors in Go →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.