Go Channels: The Communicating Conductor
Go's channels are a powerful concurrency primitive, enabling goroutines to safely communicate and synchronize. This challenge will test your understanding of channels by building a simple producer-consumer system. Imagine a scenario where one part of your program generates data, and another part processes it. Channels are the perfect tool to bridge this gap, ensuring data flows smoothly and safely between them.
Problem Description
Your task is to implement a system that simulates a producer generating numbers and a consumer processing them using Go channels.
What needs to be achieved:
- Producer Goroutine: Create a goroutine that generates a sequence of integers (e.g., from 1 to N).
- Consumer Goroutine: Create a separate goroutine that receives these integers from the producer.
- Channel Communication: Use a Go channel to pass the integers from the producer to the consumer.
- Synchronization: Ensure the consumer processes all numbers sent by the producer.
- Termination: The program should gracefully terminate once all numbers have been produced and consumed.
Key requirements:
- You must use at least one channel for communication.
- The producer and consumer should run concurrently as separate goroutines.
- The producer should signal to the consumer when it has finished sending all numbers.
Expected behavior:
The consumer should receive numbers in the order they were produced. Upon receiving all numbers, both the producer and consumer goroutines should complete their execution, and the main program should exit without errors.
Edge cases to consider:
- What happens if the producer generates zero numbers?
- How does the consumer handle the producer finishing?
Examples
Example 1:
Input: N = 5
Output:
Produced: 1
Consumed: 1
Produced: 2
Consumed: 2
Produced: 3
Consumed: 3
Produced: 4
Consumed: 4
Produced: 5
Consumed: 5
Explanation: The producer generates numbers 1 through 5. Each number is sent via the channel. The consumer receives each number and prints a "Consumed" message. After the producer finishes, the consumer also stops and the program terminates.
Example 2:
Input: N = 0
Output:
(No output indicating production or consumption)
Explanation: If N is 0, the producer generates no numbers. The channel remains empty, and the consumer receives no data. The program should terminate immediately after initialization.
Constraints
- The maximum value for N (the number of integers to produce) will be 1000.
- N will be a non-negative integer.
- The solution should demonstrate effective use of channels for inter-goroutine communication.
Notes
- Consider using
sync.WaitGroupto ensure the main goroutine waits for both the producer and consumer to finish before exiting. - Think about how to gracefully close the channel and how the consumer can detect that the producer has finished sending data.
- Unbuffered channels can be a good starting point, but you might also explore buffered channels. For this problem, an unbuffered channel is sufficient.