Overview

5 Working With Types

Go’s type system is presented as a practical way to model real-world problems with clarity and safety. The chapter uses a recipe application to show how named types, structs, collections, pointers, methods, and interfaces can work together to represent ingredients, measurements, steps, and complete recipes. Rather than relying on loosely structured data, the code becomes more expressive, more self-documenting, and less prone to accidental misuse.

The discussion begins by showing how user-defined types add meaning to basic values such as numbers and strings. Named types like measurements and units help distinguish concepts that would otherwise look identical to the compiler, while structs combine related fields into a single entity. The chapter also emphasizes Go’s preference for composition over inheritance, demonstrating how embedding lets one type build on another to create specialized measurement forms and richer domain models without complex class hierarchies.

From there, the chapter shows how behavior is attached to types through methods, with a useful distinction between value receivers and pointer receivers depending on whether the method should modify the original value. Interfaces then shift the focus from what a value is to what it can do, enabling polymorphism through shared method sets such as conversion behavior. Finally, type assertions and type switches are used to inspect interface values when concrete type-specific logic is needed, tying everything together into a flexible recipe system that can scale, convert units, and generate summaries in a clean, idiomatic Go style.

Type checking - allows for the compiler to determine if the code is valid prior to the system running
CH05 F01 Holmes
Type embedding - allows for the extension of structs by creating a new type from an existing type.
CH05 F02 Holmes
Pointers help programs manage shared sections of memory throughout an application
CH05 F03 Holmes

Summary

  • Go’s type system lets you model real-world domains using named types, structs, and composition for safer, more expressive code.
  • Named types and structs clarify intent, group related data, and enable attaching methods for behavior.
  • Type embedding (composition) extends types and shares fields/methods without inheritance.
  • Collection types (slices, maps) organize groups of values, supporting abstractions like ingredient lists and grocery lists.
  • Pointers allow efficient sharing and modification of data, especially for methods that update receiver state.
  • Methods and interfaces add behavior and define contracts, enabling polymorphism and flexible code.
  • Type assertions and switches let you check and handle concrete types at runtime when working with interfaces.
  • Composing and wrapping types enables you to extend functionality and add domain-specific logic.

FAQ

What is the purpose of Go’s type system in this chapter?Go’s type system is used to model real-world data and relationships more accurately. It helps you create safer, more expressive code by defining named types, structs, methods, interfaces, and collections that reflect the problem domain.
What is a named type in Go?A named type is a new, distinct type based on an existing type, such as type Magnitude float32. Even if it has the same underlying representation, Go treats it as a separate type and requires explicit conversion when mixing it with the original type.
How is a type alias different from a defined type?A type alias, written with =, gives an existing type another name without creating a new type. A defined type creates a distinct type that can have its own methods and stricter type checking.
Why are structs useful in Go?Structs let you group related values, possibly of different types, into one composite type. They are useful for modeling entities like measurements, ingredients, steps, and recipes in a clear and organized way.
What happens if you omit a field when creating a struct value?Go assigns the zero value for that field’s type. For example, an omitted numeric field becomes 0, and an omitted string field becomes an empty string.
What is type embedding in Go?Type embedding is Go’s way of composing types instead of using inheritance. By embedding one type inside another, the outer type gains access to the embedded type’s fields and methods.
Why are slices and maps wrapped in named types in this chapter?They are wrapped in named types to give collection data more meaning and structure. For example, a RecipeBox is a slice of recipes, and a GroceryList is a map from ingredient to measurement.
What is the difference between value receivers and pointer receivers?A value receiver works on a copy of the receiver, so changes do not affect the original value. A pointer receiver works on the original value, so changes made by the method persist outside the method.
What is an interface in Go?An interface defines a set of method signatures that a type must implement. It describes behavior rather than data, allowing different types to be used interchangeably if they satisfy the same contract.
How do type assertions and type switches help with interfaces?They let you inspect the concrete type stored inside an interface value at runtime. A type assertion checks for one specific type, while a type switch handles several possible types in a cleaner way.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Go in Action, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Go in Action, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Go in Action, Second Edition ebook for free