Explain the difference between a type assertion and a type switch, and when would you use one over the other?

Last updated 20 June 2026

  • A type assertion v, ok := x.(T) extracts one concrete type. Use the comma-ok form, since the single-return form panics on mismatch.

  • A type switch switch v := x.(type) branches over many types, binding v to each case's type, with a default for unknowns.

  • Use an assertion for one expected type and a switch when behavior depends on two or more types.

  • https://www.reddit.com/r/golang/comments/1ah60oa/type_assertion_vs_type_switches_in_go/
  • https://dev.to/ishmam_abir/swithing-data-types-understanding-the-type-switch-in-golang-4enc
  • https://www.nextround.in/tech/golang/Basics/explain-type-assertion-and-type-switch-with-real-world-examples-69c0fa7fdbe9d83394b3c2c7
  • https://go.dev/doc/effective_go
  • https://medium.com/@lenonrodrigues/go-tip-5-handling-interface-conversions-and-type-assertions-in-go-015b29005b47
All Golang interview questions