# Senior Go Backend Playbook

Compressed reference for the senior-only course extension. Pair with [cheat-sheet.md](./cheat-sheet.md) and [repo-go-map.md](./repo-go-map.md).

## The senior shift
Junior/intermediate Go asks: “Can you use the language?”
Senior Go asks: “Can you explain the trade-offs, failure modes, and runtime cost model?”

## The five senior lenses
1. **Allocation awareness** — know what escapes, what allocates, and what creates GC work.
2. **Bounded concurrency** — every concurrent design needs a stop condition and a limit.
3. **Failure shaping** — timeouts, retries, and cancellation must not amplify outages.
4. **Interface discipline** — small consumer-owned interfaces keep code testable and boundaries honest.
5. **Measurement before tuning** — benchmark, profile, and trace before “optimizing.”

## Backend review checklist
- Does every goroutine have a clear owner and a clear stop path?
- Can upstream cancellation stop downstream work promptly?
- Are retries bounded by timeout and concurrency limits?
- Are we retaining large buffers through small subslices?
- Are hot-path allocations measured before reaching for `sync.Pool`?
- Is transport-specific logic kept at the edge instead of leaking inward?
- Is the interface defined by the caller's needs instead of producer convenience?

## Interview answer shapes
### When would you choose a mutex over a channel?
Use a mutex when the problem is “protect this shared state.” Use a channel when the problem is “coordinate work or transfer ownership.” In Go, the simpler mental model wins.

### What does `GOMEMLIMIT` actually do?
It gives the runtime a soft memory budget. The GC works harder as the program approaches that budget so the process is less likely to be OOM-killed in constrained environments.

### Why do senior engineers care about escape analysis?
Because stack allocation is cheap and heap allocation creates GC work. Understanding escapes helps you reason about latency and memory pressure in hot paths.

### Why is `errgroup` often better than `WaitGroup`?
Because production work is usually not “just wait.” It also needs error propagation, cancellation, and often concurrency limits.

### What makes a retry strategy safe?
A retry needs bounded time, bounded concurrency, awareness of cancellation, and some thinking about idempotency or duplicate side effects.
