Dev Skills
Go tests·14 questions

Concurrency

Goroutines, channels, select, mutexes, races, and context.

1 / 14

Roughly what does a new goroutine cost?

All questions with explanations (answers shown)
  1. 1. Roughly what does a new goroutine cost?

    Answer: ~2KB of stack that grows on demand

    About 2KB, growing as needed. That is why running a hundred thousand goroutines is routine, and a hundred thousand OS threads is not.

  2. 2. What happens when you send on a closed channel?

    Answer: It panics

    Sending on a closed channel panics. Receiving from one is fine: once any buffered values are drained, you get the zero value and ok == false immediately.

  3. 3. Who should close a channel?

    Answer: The sender, and only one goroutine

    Closing means "no more values are coming", which only the sender knows. Two goroutines closing the same channel panics.

  4. 4. What does receiving from a nil channel do?

    Answer: Blocks forever

    It blocks forever, and that is useful. Setting a channel variable to nil inside a select disables that case, which is the standard way to drop a branch out of a loop.

  5. 5. Where must wg.Add(1) go?

    Answer: Before the go statement

    Inside the goroutine it races with Wait, which can see a count of zero and return before anything started.

  6. 6. go func() { ch <- expensive() }() where nobody ever receives. What have you built?

    Answer: A goroutine leak — it blocks on the send forever

    A blocked goroutine is never collected, and neither is anything it references. Fix it with a buffer of 1, or by selecting on ctx.Done().

  7. 7. Two goroutines read and write the same map with no synchronisation. What does the runtime do?

    Answer: Detects it and hard-crashes with "concurrent map writes"

    Maps are not safe for concurrent use, and the runtime crashes on purpose rather than let the map corrupt (a read racing a write reports "concurrent map read and map write"). The check is best-effort, so still run -race.

  8. 8. Where should a context.Context live?

    Answer: The first parameter of the function, named ctx

    A context belongs to one call, not to an object. Storing it in a struct means one long-lived context outlives the request it came from.

  9. 9. What is context.Value for?

    Answer: Request-scoped data crossing API boundaries — request id, user id, trace span

    If a function needs it to do its job, it is a parameter. Context values are invisible to the compiler — nothing tells you one is missing until it fails at runtime.

  10. 10. Which command finds data races?

    Answer: go test -race ./...

    The race detector adds checks to the binary and reports races that actually occur at runtime, so exercise the concurrent paths in your tests. Run it in CI.

  11. 11. When should you prefer a mutex over a channel?

    Answer: When you are guarding shared state rather than transferring ownership

    A mutex around a cache is simpler and faster than a goroutine owning a map behind a channel. Channels transfer ownership; mutexes protect fields.

  12. 12. What does a select with a default case do when no channel is ready?

    Answer: Runs default immediately

    A default makes the select non-blocking. Without one it waits until some case is ready, choosing at random among those that are.

  13. 13. ch := make(chan int, 1); ch <- 7; close(ch). What do two receives v, ok := <-ch give?

    Answer: 7 true, then 0 false

    Closing does not discard buffered values. You drain 7 with ok == true, then every later receive returns the zero value with ok == false immediately. That is how for v := range ch knows to stop.

  14. 14. In a select loop, one case receives from in, which is nil. What happens to that case?

    Answer: It is never chosen; the other cases still work

    Sending to or receiving from a nil channel blocks forever, so in a select that case simply never fires. Setting a drained channel to nil is the standard way to switch off one case while the others keep running. Only a select whose cases are *all* nil, with no default, blocks forever.