A channel in Go is a way to send and receive values between different goroutines safely and synchronously.
Click to reveal answer
beginner
How do you create an unbuffered channel of integers in Go?
Use make(chan int) to create an unbuffered channel that sends and receives integers.
Click to reveal answer
intermediate
What is the difference between buffered and unbuffered channels?
Unbuffered channels block the sender until the receiver is ready. Buffered channels allow sending a limited number of values without blocking.
Click to reveal answer
beginner
How do you create a buffered channel with capacity 3 for strings?
Use make(chan string, 3) to create a buffered channel that can hold 3 strings without blocking the sender.
Click to reveal answer
intermediate
Why should channels be created before using them?
Channels must be created with make before use because they are references to data structures that manage communication. Using a nil channel causes deadlock.
Click to reveal answer
How do you create an unbuffered channel of type int in Go?
Achan int{}
Bmake(chan int)
Cnew(chan int)
Dchan int()
✗ Incorrect
Use make(chan int) to create an unbuffered channel of integers.
What happens when you send to an unbuffered channel with no receiver ready?
AThe send fails immediately
BThe program crashes
CThe send is ignored
DThe send blocks until a receiver is ready
✗ Incorrect
Sending to an unbuffered channel blocks the sender until a receiver receives the value.
How do you create a buffered channel with capacity 5 for float64 values?
Achan float64(5)
Bmake(chan float64)
Cmake(chan float64, 5)
Dnew(chan float64, 5)
✗ Incorrect
Use make(chan float64, 5) to create a buffered channel with capacity 5.
What is the default value of a channel variable before creation?
Anil
Bempty channel
Czero value of element type
Dfalse
✗ Incorrect
A channel variable is nil before it is created with make.
Which keyword is used to create a channel in Go?
Amake
Bnew
Cchan
Dcreate
✗ Incorrect
Channels are created using the make keyword.
Explain how to create both unbuffered and buffered channels in Go and when you might use each.
Think about how channels control communication between goroutines.
You got /6 concepts.
Describe what happens if you try to send or receive on a nil channel in Go.
Consider what happens when a channel is not initialized.
You got /4 concepts.
Practice
(1/5)
1. What is the correct way to create an unbuffered channel of integers in Go?
easy
A. ch := chan int{}
B. ch := new(chan int)
C. ch := make(chan int)
D. ch := make(int chan)
Solution
Step 1: Understand channel creation syntax
Channels are created using the built-in make function with the syntax make(chan Type).
Step 2: Identify the correct option
ch := make(chan int) uses make(chan int), which is the correct syntax for an unbuffered integer channel.
Final Answer:
ch := make(chan int) -> Option C
Quick Check:
make(chan int) = correct channel creation [OK]
Hint: Use make(chan Type) to create channels in Go [OK]
Common Mistakes:
Using new(chan int) instead of make
Incorrect syntax like chan int{}
Swapping order in make(int chan)
2. Which of the following creates a buffered channel of strings with capacity 5?
easy
A. ch := make(chan string, cap=5)
B. ch := make(chan string, 5)
C. ch := make(chan string, "5")
D. ch := make(chan string)
Solution
Step 1: Recall buffered channel syntax
Buffered channels are created by passing a second argument to make, which is the capacity (an integer).
Step 2: Identify the correct option
ch := make(chan string, 5) correctly uses make(chan string, 5) to create a buffered string channel with capacity 5.
Final Answer:
ch := make(chan string, 5) -> Option B
Quick Check:
make(chan string, 5) = buffered channel [OK]
Hint: Add capacity as second arg in make for buffered channels [OK]
The channel has capacity 2, so it can hold two values without blocking. Values 10 and 20 are sent in order.
Step 2: Read values from channel in FIFO order
Receiving from the channel returns values in the order sent: first 10, then 20.
Final Answer:
10
20 -> Option A
Quick Check:
Buffered channel FIFO order = 10 then 20 [OK]
Hint: Buffered channels output values in send order (FIFO) [OK]
Common Mistakes:
Assuming output reverses order
Expecting runtime or compile error
Confusing buffered with unbuffered channel behavior
4. Identify the error in this Go code snippet:
ch := make(chan int)
ch <- 5
fmt.Println(<-ch)
medium
A. Deadlock: sending on unbuffered channel without receiver
B. Syntax error in channel creation
C. Cannot print channel value directly
D. No error, code runs fine
Solution
Step 1: Analyze unbuffered channel send
Unbuffered channels block the sender until a receiver is ready. Here, send happens before receive.
Step 2: Identify deadlock situation
Since no goroutine is receiving concurrently, the send blocks forever causing deadlock.
Final Answer:
Deadlock: sending on unbuffered channel without receiver -> Option A
Quick Check:
Unbuffered send blocks without receiver = deadlock [OK]
Hint: Unbuffered send blocks until receive happens [OK]
Common Mistakes:
Thinking code compiles but runs fine
Confusing syntax error with runtime deadlock
Assuming print causes error
5. You want to create a channel that can hold up to 3 float64 values without blocking the sender immediately. Which code snippet correctly does this and sends three values safely?