liveBook · Manning

Type switch

A type switch is a Go control-flow construct that selects a branch according to the dynamic type of an interface value.

How 2 Manning books explain it

Go in Practice, Second Edition

A type switch statement is a Go control-flow construct that selects a case according to the dynamic type of an interface value.

How it is used
Dispatches arbitrary interface values to type-specific operations inside generic functions.
How deep it goes
Explains syntax, conversions, parsing, unsupported types, named-type behavior, and the reflection-based kind-switch alternative.
Examples
Demonstrates a variadic mixed-value sum, string parsing, a named MyInt type, unsupported-type output, and a reflect-based kind switch.
What you can do after
Builds generic Go functions that process supported interface values and choose exact-type or underlying-kind dispatch.

Read how Go in Practice, Second Edition explains type switch →

Go in Action, Second Edition

A type switch is a Go control-flow construct that selects a case according to the dynamic type of an interface value.

How it is used
Handles type-specific behavior when an interface value may contain several concrete types.
How deep it goes
Compares interface dynamic types and contrasts multiple-case switches with single-type assertions, but does not detail runtime internals.
Examples
Uses Convertible values with MetricWeight, MetricVolume, ImperialWeight, and ImperialVolume cases to select a measurement system.
What you can do after
Apply it via Go’s type-switch syntax to route interface values into type-specific logic.

Read how Go in Action, Second Edition explains type switch →

Related concepts

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.