Skip to content
goadvanced

Go Channels In Depth

channel, select and concurrency patterns

7 questions

By EZ4Code Team

1. What is the characteristic of an unbuffered channel?

Send and receive are synchronized; the sender blocks until there is a receiver
Send does not block
Can buffer any amount of data
Does not need a receiver
Explanation: An unbuffered channel (ch := make(chan int)) blocks on send until the other end receives, often used for goroutine synchronization.

2. What happens when you close an already closed channel?

panic
No operation
Returns an error
Blocks
Explanation: Closing a channel twice triggers a panic; typically only the sole sender should close a channel.

3. What happens when receiving from a closed channel?

Returns remaining values; once exhausted, returns zero value with ok as false
panic
Blocks forever
Returns an error
Explanation: Receiving from a closed channel returns buffered remaining values; once the buffer is exhausted, it returns the zero value with the second return value ok as false, without panicking or blocking.

4. What is the behavior of the select statement?

Randomly selects one ready case to execute
Selects the first ready case in order
Selects the last case
Requires all cases to be ready
Explanation: select randomly chooses one ready case to execute; if no case is ready and there is a default, it executes default; otherwise it blocks.

5. What happens with the following code? ch := make(chan int) ch <- 1

ch := make(chan int)
ch <- 1
Deadlock: unbuffered channel with no receiver, sender blocks forever
Executes normally
panic
Returns 1
Explanation: Sending on an unbuffered channel requires a receiver to be ready; the main goroutine sending alone with no receiver blocks forever, and the Go runtime detects all goroutines blocked and reports a deadlock.

6. What is typically used to implement the worker pool pattern?

jobs channel for input + results channel for output + a fixed number of worker goroutines
A single goroutine processing sequentially
Shared global variables
An infinite select loop
Explanation: Worker pool pattern: start a fixed number of workers reading tasks from a jobs channel, writing results to a results channel, to control concurrency.

7. What does the fan-out / fan-in pattern describe?

fan-out: multiple goroutines process the same input; fan-in: merge multiple results into one channel
fan-out is merging, fan-in is distributing
They are the same
fan-in is distributing
Explanation: fan-out starts multiple goroutines to process input; fan-in merges outputs from multiple goroutines through a single converging channel.

More go Quizzes