| 214 | } |
| 215 | |
| 216 | func ExampleTeeWithDone() { |
| 217 | done := make(chan struct{}) |
| 218 | pipe := make(chan int) |
| 219 | |
| 220 | ch1, ch2, _ := harmony.Tee(done, pipe) |
| 221 | |
| 222 | // generator |
| 223 | go func() { |
| 224 | defer close(pipe) |
| 225 | for i := 1; i <= 10; i++ { |
| 226 | pipe <- i |
| 227 | } |
| 228 | }() |
| 229 | |
| 230 | // consumers |
| 231 | consumer := func(ch <-chan int, wg *sync.WaitGroup, consum func(i int)) { |
| 232 | defer wg.Done() |
| 233 | |
| 234 | for k := range ch { |
| 235 | consum(k) |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | sum, prod := 0, 1 |
| 240 | wg := sync.WaitGroup{} |
| 241 | wg.Add(2) |
| 242 | |
| 243 | go consumer(ch1, &wg, func(i int) { sum += i }) // Sum |
| 244 | go consumer(ch2, &wg, func(i int) { prod *= i }) // Product/Factorial |
| 245 | |
| 246 | wg.Wait() |
| 247 | fmt.Printf("Sequence sum is %d. Sequence product is %d", sum, prod) |
| 248 | // Output: Sequence sum is 55. Sequence product is 3628800 |
| 249 | } |
| 250 | |
| 251 | func ExampleWorkerPoolWithContext_Primes() { |
| 252 | ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond) |