Skip to content

Commit d717978

Browse files
committed
concurrency/generator: merge the source and the pattern files
1 parent 89df9f0 commit d717978

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

concurrency/generator.go

-24
This file was deleted.

concurrency/generator.md

+34-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
11
# Generator Pattern
22

3-
[Generators](https://en.wikipedia.org/wiki/Generator_(computer_programming)) yields a sequence of values one at a time
3+
[Generators](https://en.wikipedia.org/wiki/Generator_(computer_programming)) yields a sequence of values one at a time.
44

5-
# Implementation and Example
5+
## Implementation
66

7-
You can find the implementation and usage in [generator.go](generator.go)
7+
```go
8+
func Count(start int, end int) chan int {
9+
ch := make(chan int)
10+
11+
go func(ch chan int) {
12+
for i := start; i < end ; i++ {
13+
// Blocks on the operation
14+
ch <- result
15+
}
16+
17+
close(ch)
18+
}(ch)
19+
20+
return ch
21+
}
22+
```
23+
24+
## Usage
25+
26+
```go
27+
fmt.Println("No bottles of beer on the wall")
28+
29+
for i := range Count(1, 99) {
30+
fmt.Println("Pass it around, put one up,", i, "bottles of beer on the wall")
31+
// Pass it around, put one up, 1 bottles of beer on the wall
32+
// Pass it around, put one up, 2 bottles of beer on the wall
33+
// ...
34+
// Pass it around, put one up, 99 bottles of beer on the wall
35+
}
36+
37+
fmt.Println(100, "bottles of beer on the wall")
38+
```

0 commit comments

Comments
 (0)