A type switch is a Go control-flow construct that compares an interface value’s dynamic type with multiple cases. It supports type-specific logic while preserving an interface-based abstraction. section 5.6 section 5.0
Use a type switch when several concrete types require different handling. A single type assertion is more direct when only one concrete type matters; a default case can handle unexpected types. section 5.6
Classifying convertible measurements
A Convertible interface can contain metric or imperial measurements. The switch groups MetricWeight and MetricVolume under Metric, and ImperialWeight and ImperialVolume under Imperial, returning an empty value for unknown types. section 5.6
func MeasurementSystemFromConvertible(m Convertible) MeasurementSystem {
switch m.(type) {#1
case MetricWeight, MetricVolume:
return Metric
case ImperialWeight, ImperialVolume:
return Imperial
default:
// Optionally handle unknown types
return ""
}
}
Inspect the complete function pattern for switching on a Convertible value and grouping several concrete types in one case. section 5.6

