type switch
A switch statement written switch v := value.(type) that picks a branch by the concrete type inside an interface value.
In more detail
Each case names a type, and inside it v already has that type. A default case handles everything else. It is the tidy way to handle a small, known set of possible types.
Example
switch item := value.(type) {
case string:
case int:
default:
}Related terms
- type assertion — An expression such as value.(string) that gets the concrete value out of an interface, if it really holds that type.
- empty interface — The interface with no methods, spelled any or interface{}, which every Go type satisfies.
- interface value — A value held in an interface variable, which carries two parts: the concrete type it really is and the value itself.
Taught in
- Interfaces and Why They Are Implicit — Concurrent Systems Programming with Go
- Type Assertions and the Empty Interface — Concurrent Systems Programming with Go