Channel synchronization in Go is a way to coordinate the execution of goroutines by sending and receiving values through channels, ensuring that operations happen in a controlled order.
Click to reveal answer
beginner
How does sending and receiving on a channel synchronize goroutines?
When a goroutine sends a value on a channel, it waits until another goroutine receives that value. This waiting creates synchronization between the two goroutines.
Click to reveal answer
beginner
What happens if a goroutine tries to receive from an empty channel?
The goroutine blocks (waits) until another goroutine sends a value into the channel, ensuring synchronization.
Click to reveal answer
intermediate
Explain the difference between buffered and unbuffered channels in synchronization.
Unbuffered channels block the sender until the receiver is ready, providing direct synchronization. Buffered channels allow sending multiple values without an immediate receiver, so synchronization depends on buffer capacity.
Click to reveal answer
beginner
What is a common use of channel synchronization in Go programs?
A common use is to wait for goroutines to finish their work by sending a signal on a channel, allowing the main goroutine to synchronize and continue only after others complete.
Click to reveal answer
What happens when a goroutine sends a value on an unbuffered channel and no goroutine is ready to receive?
AThe sending goroutine blocks until a receiver is ready.
BThe value is lost and the goroutine continues.
CThe channel buffers the value automatically.
DThe program crashes.
✗ Incorrect
Sending on an unbuffered channel blocks the sender until another goroutine receives the value, ensuring synchronization.
Which type of channel allows sending multiple values without immediate receiver?
AUnbuffered channel
BBuffered channel
CNil channel
DClosed channel
✗ Incorrect
Buffered channels have capacity to hold multiple values, so sending can proceed without immediate receiving until the buffer is full.
If a goroutine tries to receive from an empty channel, what happens?
AIt blocks until a value is sent.
BIt causes a deadlock error.
CIt skips the receive operation.
DIt receives a zero value immediately.
✗ Incorrect
Receiving from an empty channel blocks the goroutine until another goroutine sends a value, enabling synchronization.
How can channels be used to wait for multiple goroutines to finish?
ABy using global variables.
BBy closing the channel before goroutines start.
CBy ignoring channel operations.
DBy sending a signal on the channel when each goroutine finishes.
✗ Incorrect
Each goroutine sends a signal on the channel when done, allowing the main goroutine to receive these signals and wait for completion.
What does it mean when we say channel operations are blocking?
AThey run in the background without waiting.
BThey stop the program completely.
CThey pause the goroutine until the operation can proceed.
DThey cause errors if no other goroutine is ready.
✗ Incorrect
Blocking means the goroutine pauses at the channel operation until the other side is ready, enabling synchronization.
Describe how unbuffered channels synchronize two goroutines.
Think about what happens when one goroutine sends and the other receives.
You got /4 concepts.
Explain the difference between buffered and unbuffered channels in terms of synchronization.
Consider how the buffer changes waiting behavior.
You got /4 concepts.
Practice
(1/5)
1. What happens when a goroutine tries to send data on an unbuffered channel if no other goroutine is ready to receive?
easy
A. The sending goroutine waits until another goroutine receives the data.
B. The data is lost and the program continues immediately.
C. The program crashes with a runtime error.
D. The data is buffered automatically for later use.
Solution
Step 1: Understand unbuffered channel behavior
Unbuffered channels require both sender and receiver to be ready at the same time for communication.
Step 2: Analyze sending on unbuffered channel
If no goroutine is ready to receive, the sending goroutine blocks and waits until a receiver is ready.
Final Answer:
The sending goroutine waits until another goroutine receives the data. -> Option A
Quick Check:
Unbuffered channel send blocks until receive [OK]
Hint: Unbuffered sends block until a receiver is ready [OK]
Common Mistakes:
Thinking data is buffered automatically
Assuming send never blocks
Believing program crashes on send
2. Which of the following is the correct syntax to create a buffered channel of integers with capacity 3 in Go?
easy
A. ch := chan int{3}
B. ch := make(chan int)
C. ch := make(chan int, 3)
D. ch := new(chan int, 3)
Solution
Step 1: Recall channel creation syntax
Channels are created using make with type and optional buffer size: make(chan Type, capacity).
Step 2: Identify correct buffered channel syntax
To create a buffered channel of int with capacity 3, use make(chan int, 3).
A goroutine sends "hello" on an unbuffered channel ch, while main receives from ch.
Step 2: Understand synchronization
The send and receive synchronize, so main receives "hello" and prints it.
Final Answer:
hello -> Option D
Quick Check:
Send and receive synchronize, printing message [OK]
Hint: Unbuffered channel send/receive synchronize to pass data [OK]
Common Mistakes:
Expecting deadlock because of goroutine
Thinking channel is buffered by default
Assuming no output due to concurrency
4. Identify the error in this Go code snippet involving channel synchronization:
package main
func main() {
ch := make(chan int)
ch <- 5
val := <-ch
println(val)
}
medium
A. No error, code runs fine.
B. Sending on channel blocks forever causing deadlock.
C. Channel is not initialized properly.
D. Receiving from channel blocks forever causing deadlock.
Solution
Step 1: Check channel creation and usage
Channel ch is unbuffered and created correctly with make(chan int).
Step 2: Analyze send operation
Sending on unbuffered channel blocks until another goroutine receives. Here, send happens before receive, no other goroutine receives, so it blocks forever.
Final Answer:
Sending on channel blocks forever causing deadlock. -> Option B
Quick Check:
Send blocks without concurrent receiver [OK]
Hint: Send on unbuffered channel blocks if no receiver ready [OK]
Common Mistakes:
Thinking receive blocks first
Assuming channel is nil
Believing code runs without deadlock
5. You want to run three tasks concurrently and wait for all to finish before exiting. Which Go code snippet correctly uses channels to synchronize all tasks?
hard
A. Use a buffered channel with capacity 3; each goroutine sends a signal when done; main receives 3 signals.
B. Use an unbuffered channel; main sends signals to goroutines to start; no synchronization on finish.
C. Use a buffered channel with capacity 1; goroutines send signals; main receives once.
D. Use no channels; just start goroutines and exit main immediately.
Solution
Step 1: Understand synchronization goal
Main must wait for all 3 goroutines to finish before exiting.
Step 2: Analyze channel usage options
Buffered channel with capacity 3 allows all goroutines to send done signals without blocking; main receives 3 signals to know all finished.
Final Answer:
Use a buffered channel with capacity 3; each goroutine sends a signal when done; main receives 3 signals. -> Option A
Quick Check:
Buffered channel capacity = number of goroutines to sync [OK]
Hint: Use buffered channel sized to goroutines for sync signals [OK]
Common Mistakes:
Using unbuffered channel causing deadlock
Receiving fewer signals than goroutines
Not waiting for goroutines at all
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.