mirror of
https://github.com/tmrts/go-patterns.git
synced 2026-11-11 23:07:51 +03:00
concurrency/generator: refactor generator pattern
This commit is contained in:
1 parent
d05638adac
commit
89df9f0955
2 files changed
+27
-2
No files matched your search
@@ -0,0 +1,24 @@
|
||||
package generator
|
||||
|
||||
func Range(start int, end int, step int) chan int {
|
||||
c := make(chan int)
|
||||
|
||||
go func() {
|
||||
result := start
|
||||
for result < end {
|
||||
c <- result
|
||||
result = result + step
|
||||
}
|
||||
|
||||
close(c)
|
||||
}()
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func main() {
|
||||
// print the numbers from 3 through 47 with a step size of 2
|
||||
for i := range Range(3, 47, 2) {
|
||||
println(i)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user