PrepPilotPractise free

Go Concurrency interview questions

Goroutines, channels, select, context, sync

18 questions · goroutine · updated November 2026

Practise these as flashcards. One or two lines each; the ones you miss come back first.

Start free

Core questions

What is a goroutine in Go, and how do you start one?

A goroutine is a lightweight thread managed by the Go runtime, allowing functions to run concurrently. You start a goroutine by prefixing a function call with the go keyword, like go myFunc().

What is the difference between a buffered and an unbuffered channel?

An unbuffered channel blocks the sender until a receiver is ready, ensuring synchronous communication. A buffered channel allows sending up to its capacity without blocking, enabling asynchronous communication between goroutines.

How can you implement a timeout when waiting for a value from a channel?

You can use the select statement with a time.After channel to implement a timeout. For example, select { case v := <-ch: ...; case <-time.After(time.Second): ... } will wait for either a value or a timeout.

What does sync.WaitGroup do?

sync.WaitGroup is used to wait for a collection of goroutines to finish. You call Add before starting goroutines, Done in each goroutine when it completes, and Wait to block until all have finished.

What does the select statement do in Go?

select lets you wait on multiple channel operations, proceeding with whichever is ready first. It's useful for implementing timeouts, multiplexing, and handling multiple concurrent events.

Intermediate questions

How does the Go scheduler multiplex goroutines onto OS threads?

The Go scheduler uses a work-stealing algorithm to multiplex thousands of goroutines onto a smaller set of OS threads. It manages goroutine states and schedules them based on availability of threads and I/O, allowing efficient concurrent execution.

When would you use a buffered channel over an unbuffered one?

Buffered channels are useful when you want to decouple the sender and receiver, allowing the sender to proceed without blocking until the buffer is full. This is common in producer-consumer scenarios or when batching messages for efficiency.

How do you use context.Context to cancel a long-running goroutine?

You pass a context.Context to the goroutine, and inside the goroutine, you check <-ctx.Done() or ctx.Err() to detect cancellation. When you call the cancel function, the goroutine can exit early, preventing leaks.

What is the purpose of sync.Once?

sync.Once ensures that a particular function is executed only once, even if called from multiple goroutines. It's typically used for one-time initialization, like setting up a singleton resource.

How do you prevent goroutine leaks in a fan-out worker pool?

You should ensure that all worker goroutines have a way to exit, typically by closing the work channel or sending a cancellation signal via context. Properly draining channels and handling context cancellation prevents leaks.

How does the Go race detector help in concurrent programming?

The race detector (go run -race) identifies data races at runtime by tracking variable accesses and reporting conflicts. It helps catch subtle concurrency bugs that can lead to unpredictable behavior.

How would you implement graceful shutdown for a Go HTTP server?

You use http.Server.Shutdown with a context to allow in-flight requests to complete before the server stops. Typically, you listen for OS signals, then call Shutdown to start the graceful shutdown process.

How do you ensure all goroutines in a worker pool exit when the main program is done?

See the answer in the app, free

Senior questions

Describe a scenario where using sync.Mutex could cause a deadlock, and how would you avoid it?

See the answer in the app, free

How would you design a fan-in pattern to merge results from multiple goroutines?

See the answer in the app, free

What are the trade-offs between using channels and mutexes for synchronization?

See the answer in the app, free

How would you debug a goroutine leak in a production Go service?

See the answer in the app, free

Explain how you would use atomic operations for a counter and why you might prefer them over a mutex.

See the answer in the app, free

More Go topics