Skip to content

Commit

Permalink
Update the examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
creachadair committed Mar 18, 2024
1 parent 757b896 commit e358024
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,45 +189,41 @@ func ExampleCollector() {

// Wait for the searchers to finish, then signal the collector to stop.
g.Wait()
c.Wait()

// Now get the final result.
fmt.Println(total)
// Output:
// 325
}

func ExampleCollector_Stream() {
func ExampleCollector_Report() {
type val struct {
who string
v int
}
c := taskgroup.NewCollector(func(z val) { fmt.Println(z.who, z.v) })

err := taskgroup.New(nil).
// The Stream method passes its argument a channel where it may report
// multiple values to the collector.
Go(c.Stream(func(zs chan<- val) error {
// The Report method passes its argument a function to report multiple
// values to the collector.
Go(c.Report(func(report func(v val)) error {
for i := 0; i < 3; i++ {
zs <- val{"even", 2 * i}
report(val{"even", 2 * i})
}
return nil
})).
// Multiple streams are fine.
Go(c.Stream(func(zs chan<- val) error {
// Multiple reporters are fine.
Go(c.Report(func(report func(v val)) error {
for i := 0; i < 3; i++ {
zs <- val{"odd", 2*i + 1}
report(val{"odd", 2*i + 1})
}
// An error reported by a stream is propagated just like any other
// task error.
// An error from a reporter is propagated like any other task error.
return errors.New("no bueno")
})).
Wait()
if err == nil || err.Error() != "no bueno" {
log.Fatalf("Unexpected error: %v", err)
}

c.Wait()
// Unordered output:
// even 0
// odd 1
Expand Down

0 comments on commit e358024

Please sign in to comment.