Concurrency
Goroutines, channels, select, mutexes, races, and context.
Roughly what does a new goroutine cost?
All questions with explanations (answers shown)
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. 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 == falseimmediately.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. What does receiving from a nil channel do?
Answer: Blocks forever
It blocks forever, and that is useful. Setting a channel variable to
nilinside aselectdisables that case, which is the standard way to drop a branch out of a loop.5. Where must
wg.Add(1)go?Answer: Before the
gostatementInside the goroutine it races with
Wait, which can see a count of zero and return before anything started.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. 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. Where should a
context.Contextlive?Answer: The first parameter of the function, named
ctxA 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. What is
context.Valuefor?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. 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. 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. What does a
selectwith adefaultcase do when no channel is ready?Answer: Runs
defaultimmediatelyA
defaultmakes theselectnon-blocking. Without one it waits until some case is ready, choosing at random among those that are.13.
ch := make(chan int, 1); ch <- 7; close(ch). What do two receivesv, ok := <-chgive?Answer:
7 true, then0 falseClosing does not discard buffered values. You drain
7withok == true, then every later receive returns the zero value withok == falseimmediately. That is howfor v := range chknows to stop.14. In a
selectloop, one case receives fromin, which isnil. 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
selectthat case simply never fires. Setting a drained channel tonilis the standard way to switch off one case while the others keep running. Only aselectwhose cases are *all* nil, with nodefault, blocks forever.