Go interface exercises
Go interfaces are satisfied implicitly, but the 'typed nil' trap is infamous: an interface value is only nil if both its type and value are nil. Storing a nil concrete pointer inside an interface makes it non-nil, passing nil checks and panicking later.
Master Go interface semantics and pointer receivers through real debugging challenges.
The interfaces bugs you will meet
- An interface containing a nil pointer is NOT nil (err != nil passes).
- Type assertion val.(T) panics on mismatch unless using the comma-ok idiom: val, ok := i.(T).
- Methods with pointer receivers (*T) cannot be called on value types inside interface values.
- Assigning to an entry in a nil map panics; maps inside structs must be initialized.
The exercises
Medium
- Unexpected behavior with nil interface comparisonNull and undefined
- Increasing memory usage in periodic task schedulerLogic
- Worker function not exiting properly causing timeoutsAsync and race conditions
- Panic on interface type assertion in GoType
- Custom error type returning non-nil error on nil interfaceNull and undefined
- Interface nil comparison bug with typed nilNull and undefined
Errors you might be searching for
More JavaScript practice by topic
Questions
Why is (interface)(nil) != (concrete)(nil)?
An interface in Go holds two 64-bit words: a type descriptor pointer and a data pointer. If the type is set, the interface itself is not nil.