mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-22 04:36:02 +03:00
added iterator
This commit is contained in:
parent
5d35abc39d
commit
f3daa894af
24
concurrency/iterator/main.go
Normal file
24
concurrency/iterator/main.go
Normal file
@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Generator func which produces data which might be computationally expensive.
|
||||
func fib(n int) chan int {
|
||||
c := make(chan int)
|
||||
go func() {
|
||||
for i, j := 0, 1; i < n; i, j = i+j, i {
|
||||
c <- i
|
||||
}
|
||||
close(c)
|
||||
}()
|
||||
return c
|
||||
}
|
||||
|
||||
func main() {
|
||||
// fib returns the fibonacci numbers lesser than 1000
|
||||
for i := range fib(1000) {
|
||||
// Consumer which consumes the data produced by the generator, which further does some extra computations
|
||||
v := i * i
|
||||
fmt.Println(v)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user