liveBook · Manning

Ingredient

As explained in Go in Action, Second Edition

Ingredient is a recipe-domain type representing the name of an ingredient as a distinct named value.

Ingredient is a named Go type based on string. It represents an ingredient name as a distinct domain value, making recipe code more expressive and helping prevent accidental use of an unrelated string. section 5.2.2 section 5.1

type Ingredient string

type IngredientMeasurement struct {
    Ingredient  Ingredient
    Measurement Convertible #1
}

type IngredientList []IngredientMeasurement #2

type Step struct {
    Description string
    Ingredients IngredientList
}

The declaration places Ingredient inside a recipe model, where IngredientMeasurement pairs each name with a Convertible measurement and IngredientList groups those values. section 5.2.2

The type supports a clear recipe-domain structure:

  • IngredientMeasurement associates an Ingredient with a Convertible measurement. section 5.2.2
  • IngredientList stores measured ingredients for a recipe step. section 5.2.2
  • GroceryList uses Ingredient as a map key so quantities can be accumulated by ingredient name. section 5.2.2

Ingredients in a pancake recipe

A recipe can contain Ingredient values such as Flour, Sugar, Milk, and Egg, each paired with a metric measurement. Scaling the recipe and creating a grocery list then operate on those structured values. section 5.6.2

How Go in Action, Second Edition covers it

How deep it goes
Defines Ingredient as a named string and uses it in structs, slices, and maps without descending into implementation internals.
Examples
Uses flour, sugar, milk, and egg in a pancake recipe whose ingredients are scaled and aggregated into a grocery list.
What it assumes
Assumes basic Go type declarations and string values while demonstrating domain modeling from those primitives.
What you can do after
Apply via Go's type system a distinct ingredient value in structured recipe data and ingredient-keyed collections.
How it connects
Connects Ingredient to named types, structs, slices, maps, interfaces, measurements, recipe steps, and grocery lists.

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.