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.
Exercises
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 one
Pick the IDIOMATIC Go declaration of a CUSTOM ERROR TYPE —
04
fill blanks
Type the keyword that turns a type assertion into a TYPE
05
fill blanks
Type the conventional name for the COMMA-OK boolean — the
06
type one line
Productive use of comma-ok type assertion on a USER-DEFINED
07
type one line
Type-switch on a STDLIB error type. Scaffold calls
08
write a program
Define a CUSTOM ERROR TYPE and use it productively.
09
write a program
Theme 5.3 capstone — TYPE-SWITCH multi-branch dispatch on