liveBook · Manning

Chapter 5: Working with types

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

This chapter introduction explains why Go’s type system helps model real-world domains safely and previews the type features used in a recipe application.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduction explains why Go’s type system helps model real-world domains safely and previews the type features used in a recipe application.

Key terms: primitive types, type system, composite types, domain, type safety, type checking, compile time, composition over inheritance, recipe application, methods

5.1 Modeling a domain with types

Introduces a recipe application to show how custom Go types can represent domain data clearly and safely.

Key terms: domain, recipe, ingredients, measurements, steps, type keyword, named types, underlying Go type, type literal, structs

5.1.1 Named types

This section introduces named types and typed constants to model recipe measurements more clearly and catch type mistakes at compile time.

Key terms: named type, defined type, type alias, underlying representation, explicit conversion, Magnitude, Unit, measurement, metric, imperial

5.1.2 Structs

This section shows how Go structs combine related values into a domain type, how to use their fields, and what happens when fields are omitted.

Key terms: struct, Measurement, Magnitude, Unit, struct type, field, dot notation, exported, unexported, zero value

5.2 Extending types

This section introduces type embedding as Go’s alternative to traditional object-oriented inheritance.

Key terms: inheritance, child, subclass, parent, superclass, type embedding

5.2.1 Type embedding

This subsection shows how embedding a general Measurement type creates specialized measurement types that retain its fields and methods while allowing their own behavior.

Key terms: type embedding, embedded type, fields, methods, Measurement, MetricWeight, ImperialVolume, measurement system, unit

5.2.2 Complex types

This section models recipes with named slice and map types, then uses those types to consolidate ingredients into a grocery list.

Key terms: Ingredient, IngredientMeasurement, IngredientList, Step, Recipe, RecipeBox, GroceryList, Measurement, Convertible, ToMetric()

5.3 Pointer types

This section contrasts Go’s default copying behavior with pointers, then shows how pointers share access to values and when they are useful.

Key terms: copy by value, pointer, memory address, dereferencing, *, &, large structs, arrays, nil, new

5.4 Methods and behavior

This section shows how to attach behavior to Go types with methods, choose value or pointer receivers, and see why interfaces are needed to describe capabilities independently of concrete types.

Key terms: method, receiver, func, value receiver, pointer receiver, ImperialWeight, ToMetric, MetricWeight, Recipe, methods

5.5 Interfaces

This section explains how Go interfaces define shared behavior and uses Stringer and measurement conversion to show implicit implementation in practice.

Key terms: interface, method signatures, satisfies, structural typing, compile time, Stringer, String(), fmt.Printf, -er, Convertible

5.6 Checking types

The section shows how to inspect the concrete type held by a Go interface value when code needs type-specific behavior.

Key terms: type assertion, interface value, concrete type, ok, type switch, dynamic type, Convertible, MeasurementSystem, default case

5.6.1 Type composition and wrapping

This section shows how Go composition can wrap a measurement type to add behavior without changing the original type.

Key terms: composition, wrapping, embedding, measurement type

5.6.2 Example: Putting it all together

This section brings together the recipe application’s types and behaviors to show how Go’s type system supports a clear, extensible domain model.

Key terms: named types, structs, embedded types, collection types, metric and imperial measurements, methods, interfaces, type safety, composition over inheritance, abstraction

5.7 Summary

This summary brings together how Go types model domains, share and modify data, define behavior, and handle concrete types through interfaces.

Where this is used later

Concepts explained in this chapter

← Chapter 4: Collection types Chapter 6: Generics →

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.