Type Switches
A type switch is a clean way to perform several type assertions in a series. It’s similar to a regular switch statement, but the cases specify types instead of values.
Syntax
func printValue(v interface{}) {
switch val := v.(type) {
case int:
fmt.Printf("Int: %v\n", val)
case string:
fmt.Printf("String: %v\n", val)
default:
fmt.Printf("Unknown type: %T\n", v)
}
}
Purpose
Type switches are perfect when you need to handle a set of different concrete types that all implement the same interface (or the empty interface).