iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Quiz

Six Go questions that surface in code review. Try first; the answers explain why.

Six Go design questions

EXAMPLE
// ============================================================
// Q1) Why does this leak goroutines?
// ============================================================
// for _, url := range urls {
//   go fetch(url)
// }
//
// ANSWER: no synchronization. The goroutines may outlive main, panic on a
// closed channel, or share captured variables. Use sync.WaitGroup OR an
// errgroup with a context, and ALWAYS plumb a cancellable context to fetch.
// ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
// defer cancel()
// var wg sync.WaitGroup
// for _, u := range urls {
//   wg.Add(1)
//   go func(u string) { defer wg.Done(); fetch(ctx, u) }(u)
// }
// wg.Wait()

// ============================================================
// Q2) Why does append() sometimes mutate the original slice?
// ============================================================
// xs := []int{1, 2, 3}
// ys := xs[:2]
// ys = append(ys, 99)
//
// ANSWER: ys backs the same underlying array as xs while capacity allows.
// After append, xs may become [1, 2, 99]. To avoid, copy first or grow cap:
// ys := make([]int, 2, 3); copy(ys, xs[:2])

// ============================================================
// Q3) Map iteration order is...?
// ============================================================
// ANSWER: randomised. Go intentionally randomises map iteration to prevent
// dependence on order. If you need stable order, sort the keys explicitly.

// ============================================================
// Q4) When should you return a pointer vs a value?
// ============================================================
// ANSWER:
// - Small structs (a few words):       return value (cheap, no GC pressure)
// - Big structs:                       return pointer
// - Mutation expected by callers:      pointer
// - Shared with concurrent readers:    pointer to immutable copy
// Default to VALUE for simplicity; switch to pointer when measured otherwise.

// ============================================================
// Q5) Why is your select with default a busy loop?
// ============================================================
// ANSWER: a select with default never blocks; it returns immediately if no
// case is ready. Combine with a time.After or a short sleep, or remove the
// default and let the select block until something is ready.

// ============================================================
// Q6) When to use a buffered vs unbuffered channel?
// ============================================================
// ANSWER:
// - Unbuffered: synchronous handoff between producer and consumer
//   (when you want the producer to wait for the consumer)
// - Buffered:   limited queue to smooth bursty workloads
// Buffered channels are NOT a substitute for backpressure; design the
// pipeline so a slow consumer slows the producer, not the other way around.

// ============================================================
// Bonus — why is errors.Is(err, io.EOF) sometimes false?
// ============================================================
// ANSWER: the wrapped chain may not include io.EOF. Wrap with %w:
//   return fmt.Errorf("read: %w", err)
// Then errors.Is(err, io.EOF) walks the chain.

// ============================================================
// Scoring
// ============================================================
// 6 / 6 -> lead Go code review
// 4 / 6 -> revisit go/cheatsheet
// < 4   -> read Effective Go + the std lib net/http source

Why it matters

Always thread `context.Context` through I/O calls and ALWAYS wrap errors with `%w` for `errors.Is/As` to work. Those two habits prevent the leakiest goroutines and the most opaque error chains in Go codebases — and they cost almost nothing at the call site.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
// 3 questions per lesson.
Try it Yourself »

Discussion

Loading…