Section 14
Defer — Advanced
Now that mutexes, WaitGroups, and channels are introduced: the defer patterns that make all of them safe in concurrent code.
defer + Mutex — the most important engine pattern
always defer Unlock immediately after Lock
defer + wg.Done() — every goroutine in a WaitGroup
defer wg.Done() — first line of every tracked goroutine
defer + close(ch) — notify downstream when done
defer close — signal that no more values are coming
defer + recover — prevent one bad message from crashing the engine
wrap goroutine in recover to contain panics