typeover
curriculum

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

Type assertions & type switches

Pick the IDIOMATIC Go declaration of a CUSTOM ERROR TYPE — the pattern callers will type-assert / type-switch on with comma-ok or errors.As. Custom error types are how you carry STRUCTURED context with an error (status codes, field paths, retry hints). The pattern: a struct with relevant fields + a pointer-receiver Error() string method.

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.