Bird
Raised Fist0
Goprogramming~20 mins

Why channels are used in Go - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Channel Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Go program using channels?
Consider this Go code that uses a channel to communicate between goroutines. What will it print?
Go
package main
import (
	"fmt"
)
func main() {
	ch := make(chan string)
	go func() {
		ch <- "Hello from goroutine"
	}()
	msg := <-ch
	fmt.Println(msg)
}
AEmpty output
BCompilation error
CHello from goroutine
DDeadlock error at runtime
Attempts:
2 left
💡 Hint
Think about how channels allow communication between goroutines.
🧠 Conceptual
intermediate
1:30remaining
Why are channels important in Go concurrency?
Why do Go programmers use channels when working with goroutines?
ATo share memory safely by passing messages
BTo replace functions with asynchronous calls
CTo create new goroutines automatically
DTo speed up the program by avoiding all synchronization
Attempts:
2 left
💡 Hint
Think about how channels help avoid data races.
❓ Predict Output
advanced
1:30remaining
What happens if you send on a nil channel?
Look at this Go code snippet. What will happen when it runs?
Go
package main
func main() {
	var ch chan int
	ch <- 5
}
ACompilation error: cannot send on nil channel
BProgram deadlocks and panics at runtime
CProgram prints 5
DProgram exits silently
Attempts:
2 left
💡 Hint
A nil channel blocks forever on send or receive.
🔧 Debug
advanced
2:00remaining
Why does this Go program deadlock?
This Go program uses a channel but deadlocks. Why?
Go
package main
import "fmt"
func main() {
	ch := make(chan int)
	ch <- 10
	fmt.Println(<-ch)
}
ABecause the channel buffer is full
BBecause the channel is nil
CBecause the channel is closed before sending
DBecause the send blocks waiting for a receiver that is not ready
Attempts:
2 left
💡 Hint
Think about how unbuffered channels block on send and receive.
🚀 Application
expert
2:30remaining
How many items are in the channel buffer after this code runs?
Given this Go code, how many items remain in the channel buffer after execution?
Go
package main
import "fmt"
func main() {
	ch := make(chan int, 3)
	ch <- 1
	ch <- 2
	ch <- 3
	<-ch
	ch <- 4
	fmt.Println(len(ch))
}
A2
B1
C3
D0
Attempts:
2 left
💡 Hint
Remember that reading from the channel removes one item from the buffer.

Practice

(1/5)
1. Why are channels used in Go programming?
easy
A. To safely send and receive data between different parts of a program
B. To store large amounts of data permanently
C. To speed up the execution of a single task
D. To create graphical user interfaces

Solution

  1. Step 1: Understand the purpose of channels

    Channels are designed to let different parts of a program communicate by sending and receiving data safely.
  2. Step 2: Compare with other options

    Storing data permanently, speeding up single tasks, or creating GUIs are not the main uses of channels.
  3. Final Answer:

    To safely send and receive data between different parts of a program -> Option A
  4. Quick Check:

    Channels = Safe communication [OK]
Hint: Channels are for safe data exchange between tasks [OK]
Common Mistakes:
  • Thinking channels store data permanently
  • Confusing channels with speeding up single tasks
  • Assuming channels create user interfaces
2. Which of the following is the correct way to declare a channel for integers in Go?
easy
A. int var chan ch
B. var ch chan int
C. chan var int ch
D. var ch int chan

Solution

  1. Step 1: Recall Go channel declaration syntax

    The correct syntax is: var channelName chan dataType.
  2. Step 2: Match options with syntax

    Only var ch chan int matches this pattern correctly.
  3. Final Answer:

    var ch chan int -> Option B
  4. Quick Check:

    Channel declaration = var ch chan int [OK]
Hint: Remember: var name chan type [OK]
Common Mistakes:
  • Placing chan after variable name incorrectly
  • Mixing order of var, chan, and type
  • Using invalid syntax like chan var
3. What will be the output of this Go program?
package main
import "fmt"
func main() {
  ch := make(chan string)
  go func() { ch <- "hello" }()
  msg := <-ch
  fmt.Println(msg)
}
medium
A. hello
B. Compilation error
C. Deadlock error
D. No output

Solution

  1. Step 1: Understand channel creation and goroutine

    The channel ch is created, and a goroutine sends "hello" into it.
  2. Step 2: Receive from channel and print

    The main function receives the message from ch and prints it.
  3. Final Answer:

    hello -> Option A
  4. Quick Check:

    Channel send/receive prints message [OK]
Hint: Channel send and receive passes data safely [OK]
Common Mistakes:
  • Expecting deadlock without goroutine
  • Thinking channel must be buffered
  • Confusing syntax causing compile errors
4. Identify the error in this Go code snippet:
package main
func main() {
  ch := make(chan int)
  ch <- 5
  val := <-ch
  println(val)
}
medium
A. Channel is not created properly
B. Print statement syntax error
C. Receiving from channel is incorrect
D. Sending to channel blocks forever causing deadlock

Solution

  1. Step 1: Analyze channel usage

    The channel ch is unbuffered and no goroutine is receiving when sending 5.
  2. Step 2: Understand blocking behavior

    Sending to an unbuffered channel blocks until another goroutine receives, causing deadlock here.
  3. Final Answer:

    Sending to channel blocks forever causing deadlock -> Option D
  4. Quick Check:

    Unbuffered send without receiver = deadlock [OK]
Hint: Unbuffered send blocks without receiver goroutine [OK]
Common Mistakes:
  • Assuming channel creation is wrong
  • Thinking receive causes error
  • Misreading print statement as error
5. You want two goroutines to send numbers to a main goroutine safely and print them in order received. Which approach best uses channels for this?
hard
A. Use global variables for communication instead of channels
B. Create two channels, main receives from both without select, print in fixed order
C. Create one channel, both goroutines send numbers to it, main receives and prints
D. Use a slice shared by goroutines without synchronization

Solution

  1. Step 1: Understand safe communication with channels

    One channel shared by goroutines allows safe sending and receiving of data.
  2. Step 2: Evaluate options for ordered printing

    Main goroutine receives from one channel and prints in order messages arrive, ensuring safe and clear communication.
  3. Final Answer:

    Create one channel, both goroutines send numbers to it, main receives and prints -> Option C
  4. Quick Check:

    Single channel for multiple senders = safe communication [OK]
Hint: Use one channel for multiple senders to main [OK]
Common Mistakes:
  • Using multiple channels without select causes unordered prints
  • Using global variables risks race conditions
  • Sharing slices without locks causes errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard