Implement a Goroutine-based Fibonacci sequence generator that uses channels to produce the first n Fibonacci numbers.
HardProblem Statement
Implement a Goroutine-based Fibonacci sequence generator that uses channels to produce the first n Fibonacci numbers.
Sample Input
n = 10
Sample Output
0 1 1 2 3 5 8 13 21 34
Limits
Time: 2s · Memory: 256 MB
Code Editor
00:00
package main
import "fmt"
func fibonacci(n int) <-chan int {
ch := make(chan int)
go func() {
// Your code here
}()
return ch
}
func main() {
n := 10
fibCh := fibonacci(n)
for i := 0; i < n; i++ {
fmt.Print(<-fibCh, " ")
}
}
Input
n = 10
Expected Output
0 1 1 2 3 5 8 13 21 34