Curriculum Errors & packaging Type assertions & type switches
Type assertions & type switches
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.
9 ready
01
pick one Exercise 7 of theme 4.1 introduced type assertions (v.(T)).
02
pick one When you have an error interface value and need to BRANCH
03
pick onePick the IDIOMATIC Go declaration of a CUSTOM ERROR TYPE —
04
fill blanksType the keyword that turns a type assertion into a TYPE
05
fill blanksType the conventional name for the COMMA-OK boolean — the
06
type one lineProductive use of comma-ok type assertion on a USER-DEFINED
07
type one lineType-switch on a STDLIB error type. Scaffold calls
08
write a programDefine a CUSTOM ERROR TYPE and use it productively.
09
write a programTheme 5.3 capstone — TYPE-SWITCH multi-branch dispatch on