typeover
curriculum

Curriculum Errors & packaging Type assertions & type switches exercise 2 · mcq

Type assertions & type switches

When you have an error interface value and need to BRANCH on its concrete type, Go's TYPE SWITCH is the cleaner shape than a cascade of comma-ok assertions. Pick the IDIOMATIC type-switch syntax.

TypeScript reference
Pick the idiomatic Go translation

About this theme

v.(T) asserts that an interface value v is actually of type T. Panics if wrong. Use the comma-ok form to check first: if t, ok := v.(T); ok { ... }. For multi-branch checks, switch v := i.(type) dispatches on dynamic type.