Technical Mid Level

Explain how goroutines and channels work together. How do you prevent goroutine leaks in a long-running service?

Quick Tip

Show a concrete pattern: "Every goroutine I launch receives a context. I use select with ctx.Done() so it exits cleanly when the parent cancels. I monitor goroutine counts in production dashboards."

What good answers include

Goroutines are lightweight, multiplexed onto OS threads by the Go runtime scheduler. Channels provide typed, synchronised communication between goroutines. Goroutine leaks happen when a goroutine blocks forever on a channel send/receive or waits on a resource that never resolves. Prevention strategies: always use context.Context with timeouts or cancellation, use select with a done channel, close channels when no more values will be sent, and use tools like runtime.NumGoroutine() or pprof to monitor goroutine counts in production.

What interviewers are looking for

Core Go skill. Candidates who cannot explain goroutine lifecycle management will write services that leak memory over time. Ask for a specific leak they have diagnosed.

← All Go questions