Channel Ping-Pong
Channel Ping-Pong
Section titled “Channel Ping-Pong”Practice bidirectional communication between goroutines using channels.
Background
Section titled “Background”Channels enable goroutines to communicate and synchronize. In this exercise, you’ll implement a ping-pong pattern where two goroutines alternate sending messages back and forth.
Your Task
Section titled “Your Task”Implement a ping-pong game where:
- Two goroutines pass a ball back and forth
- Each goroutine increments the ball’s hit count
- The game ends after 5 total hits
- Print each hit as it happens
Ping-Pong
~10 mineasy
Implement alternating communication between two goroutines
Key Concepts
Section titled “Key Concepts”- Directional channels: Using
<-chanfor receive-only andchan<-for send-only - Channel range: Iterating over channel values with
for range - Coordination: Using a done channel to signal completion