Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Keywords and Statements

The type switch

Reference for the Go type switch: branching on the dynamic type of an interface value, binding the value per case, nil and multi-type cases, and pitfalls.

A type switch looks like a normal switch, but its cases are types, and it compares them with the dynamic type stored inside an interface value. It is the standard way to handle values that can be one of several types, such as the results of JSON decoding into any or the concrete errors behind an error.

The special syntax x.(type) is allowed only in a type switch. With the optional binding v := x.(type), the variable v is declared in each case with the case's type (or with the interface's type when a case lists several types).

Go
switch v := x.(type) {
case int:
	fmt.Println("int", v+1)
case string:
	fmt.Println("string", len(v))
}

Try it

Go Describing values by type

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Switching on error types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
switch x.(type) { ... }
switch v := x.(type) {
case T1:           // v has type T1
case T2, T3:       // v has the type of x
case nil:          // x is a nil interface
default:           // v has the type of x
}
Part Description
x An expression of interface type (any, error, fmt.Stringer, ...).
v Optional binding, new in every clause.
case type A concrete type, or an interface type (matches if the dynamic type implements it). nil matches a nil interface value.

Errors and edge cases:

  • x.(type) outside a type switch is a compile error: invalid syntax tree: use of .(type) outside type switch.
  • v declared and not used: if no clause uses the bound variable, the compiler complains.
  • A case type that can never match (a concrete type that does not implement x's interface) is rejected: impossible type switch case.
  • Duplicate types in cases are rejected. fallthrough is not allowed.
  • Cases are tested in order, so put specific types before general interfaces: error before fmt.Stringer only matters if a type implements both.

Usage notes

  • A nil interface and a typed nil pointer are different. case nil matches only an interface holding no dynamic type. An interface holding (*T)(nil) matches case *T.
  • Single-type cases give v the concrete type. Multi-type and default cases leave v with the interface type.
  • Interface cases allow capability checks (case io.Closer:) without naming concrete types.
  • For errors, prefer errors.Is and errors.As, which look through wrapped errors; a type switch only sees the outermost error.
  • For one check, use the comma-ok assertion: s, ok := x.(string). A plain x.(string) panics when the type is wrong.
  • Generics: a type switch needs an interface value. On a type-parameter value, convert first: switch any(v).(type).