Type switch
A type switch looks at the dynamic type stored in an interface value and runs the matching case. You write switch v := x.(type) instead of a chain of type assertions.
Detailed Explanation
You have an error or an any and you need to behave differently depending on what is inside it. A type assertion (x.(*os.PathError)) asks one question. A type switch asks several in one statement and binds the value to a name whose type depends on the case.
The syntax is a switch over .(type). switch v := err.(type) assigns v in each case to the asserted type. In case *os.PathError:, v is a *os.PathError. In case *os.LinkError:, v is that type. In default, v has the same type as the interface you switched on. You can omit the v := if you only need to test types and not use the value.
Several types in one case. case *os.PathError, *os.LinkError: matches either, but then v stays the interface type, because the two cases do not share a single concrete type. If you need the fields, split the cases.
nil is a case. case nil: matches a nil interface (both words empty). It does not match a typed-nil pointer sitting in the interface. That is the same trap as err != nil.
No fallthrough. Type switches reject fallthrough. Each case is a separate type test. Order still matters only in the sense that the first matching case runs; a type cannot match two cases.
When not to use one. If you are switching on error, prefer errors.As and errors.Is. They walk a wrapped chain. A type switch sees only the outermost dynamic type. If you are switching on many concrete types you own, consider giving them a shared method instead: the interface is the switch, and new types do not require a new case in every caller.
What it costs. A type switch is a runtime walk of case types. For a handful of cases it is cheap. It is still a closed list: adding a type means finding every switch. That is the maintainability cost, not the CPU cost.
Code Examples
package main
import (
"fmt"
"os"
)
func classify(err error) string {
switch v := err.(type) {
case nil:
return "ok"
case *os.PathError:
return "path: " + v.Path
case *os.LinkError:
return "link: " + v.Old
default:
return err.Error()
}
}
func main() {
fmt.Println(classify(nil))
}func asAny(x any) {
switch x.(type) {
case int, int64:
fmt.Println("integer")
case string:
fmt.Println("string")
default:
fmt.Println("other")
}
}Prefer errors.As when the error may be wrapped.
// Prefer errors.As when the error may be wrapped.
func isPath(err error) bool {
var pe *os.PathError
return errors.As(err, &pe)
}Common Gotchas
`fallthrough` is illegal in a type switch
each case is a type match, not a label you can slide into.
Multi-type cases keep the interface type
`case A, B:` does not give you `A`'s fields. Split the cases if you need them.
Wrapped errors do not match the inner type
`fmt.Errorf("open: %w", pathErr)` will not hit `case *os.PathError`. Use `errors.As`.
`case nil` is the nil interface, not a nil pointer of some type
a `*T` nil inside `any` matches `case *T`, not `case nil`.