MCPcopy Create free account
hub / github.com/butuzov/harmony / ExampleTeeWithDone

Function ExampleTeeWithDone

examples_test.go:216–249  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

214}
215
216func 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
251func ExampleWorkerPoolWithContext_Primes() {
252 ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)

Callers

nothing calls this directly

Calls 1

TeeFunction · 0.92

Tested by

no test coverage detected