0
0
Goprogramming~5 mins

Buffered and unbuffered channels in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an unbuffered channel in Go?
An unbuffered channel in Go is a channel with no capacity to hold data. It requires both sender and receiver to be ready at the same time for communication to happen, ensuring synchronization.
Click to reveal answer
beginner
What is a buffered channel in Go?
A buffered channel in Go has a capacity to hold a fixed number of values. Senders can send data without waiting for a receiver until the buffer is full, allowing asynchronous communication up to the buffer size.
Click to reveal answer
beginner
How do you create a buffered channel in Go?
You create a buffered channel using the make function with a capacity argument, like: ch := make(chan int, 3). This channel can hold 3 integers before blocking the sender.
Click to reveal answer
intermediate
What happens when you send to a full buffered channel?
When you send to a buffered channel that is full, the sending goroutine blocks until some data is received from the channel, freeing space in the buffer.
Click to reveal answer
intermediate
Why use buffered channels instead of unbuffered channels?
Buffered channels allow some decoupling between sender and receiver, letting the sender continue without waiting immediately. This can improve performance and avoid deadlocks in some cases.
Click to reveal answer
What does an unbuffered channel guarantee in Go?
ASender and receiver synchronize on each send and receive
BSender can send multiple values without blocking
CChannel can hold multiple values in a queue
DSender never blocks
How do you declare a buffered channel with capacity 5 in Go?
Ach := make(chan int)
Bch := chan int{5}
Cvar ch chan int = 5
Dch := make(chan int, 5)
What happens if you send to a buffered channel that is not full?
ASend blocks until buffer is empty
BSend proceeds immediately without blocking
CSend blocks until receiver is ready
DSend causes a runtime error
Which of these is true about buffered channels?
AThey always block the sender
BThey never block the receiver
CThey can hold multiple values up to their capacity
DThey are slower than unbuffered channels
Why might you choose an unbuffered channel?
ATo ensure sender and receiver synchronize on each message
BTo store many messages in a queue
CTo avoid blocking the sender
DTo allow asynchronous communication
Explain the difference between buffered and unbuffered channels in Go.
Think about whether the sender waits or not.
You got /4 concepts.
    Describe a situation where using a buffered channel is better than an unbuffered channel.
    Consider when you want to let sender continue without waiting.
    You got /3 concepts.