liveBook · Manning

Type system

As explained in Go in Action, Second Edition

A type system is the set of rules governing the classification of values and the validity of operations on them.

A recipe is more than a collection of strings and numbers: ingredients, measurements, steps, and yields have different roles. A type system supplies rules for defining and using those values. In Go, named types and structs make domain concepts explicit, while strict compile-time checking catches many mismatches before a program runs. section 5.0 section 5.1

Start with named types where a primitive alone does not express intent. Compose structs to group related values, then use slices and maps for collections such as ingredient lists and grocery lists. This approach builds richer models from simpler types rather than relying on inheritance. section 5.1 section 5.2.2

type Ingredient string

type IngredientMeasurement struct {
    Ingredient  Ingredient
    Measurement Convertible #1
}

type IngredientList []IngredientMeasurement #2

type Step struct {
    Description string
    Ingredients IngredientList
}

The type definitions show the progression from an ingredient name to a measured ingredient and a step containing a list of them. The Convertible field gives measurements a shared behavioral contract. section 5.2.2

Methods add behavior to types; interfaces specify behavior that different types can share. In the recipe model, measurement types support conversion between unit systems, and recipe methods support scaling. Pointers matter when methods modify receiver state. When an interface value’s concrete type matters at runtime, type assertions and type switches provide checks. section 5.6.2 section 5.7

From recipe to grocery list

A pancake recipe contains steps with measured ingredients. After scaling the recipe, CreateGroceryList walks its steps, converts measurements to metric values, and combines amounts by ingredient. Named collection types keep the resulting grocery list distinct in purpose from the recipe’s steps. section 5.6.2 section 5.2.2

How Go in Action, Second Edition covers it

How deep it goes
Works through named types, structs, collections, methods, interfaces, pointers, and runtime checks without detailing compiler internals.
Examples
Builds a recipe model and uses a pancake recipe to demonstrate scaling, measurement conversion, and grocery-list creation.
What it assumes
Assumes familiarity with primitive values such as integers, Booleans, and strings; introduces custom types through a domain model.
What you can do after
Build it from scratch: model a recipe domain with named types, collections, methods, and interfaces.
How it connects
Connects composition with interfaces, methods, pointers, type assertions, type switches, slices, and maps.

Also explained in

Compare how 6 books explain type system →

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.