diff --git a/README.md b/README.md index 84bd839..4cd3dde 100644 --- a/README.md +++ b/README.md @@ -187,7 +187,8 @@ A task is expressed as a `func() error`, and is added to a group using the `Go` method: ```go -g := taskgroup.New(nil).Go(myTask) +var g taskgroup.Group +g.Go(myTask) ``` Any number of tasks may be added, and it is safe to do so from multiple diff --git a/example_test.go b/example_test.go index 88a5f5c..1d62cf3 100644 --- a/example_test.go +++ b/example_test.go @@ -187,24 +187,24 @@ func ExampleCollector_Report() { } c := taskgroup.Collect(func(z val) { fmt.Println(z.who, z.v) }) - err := taskgroup.New(nil). - // 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 := range 3 { - report(val{"even", 2 * i}) - } - return nil - })). - // Multiple reporters are fine. - Go(c.Report(func(report func(v val)) error { - for i := range 3 { - report(val{"odd", 2*i + 1}) - } - // An error from a reporter is propagated like any other task error. - return errors.New("no bueno") - })). - Wait() + g := taskgroup.New(nil) + // The Report method passes its argument a function to report multiple + // values to the collector. + g.Go(c.Report(func(report func(v val)) error { + for i := range 3 { + report(val{"even", 2 * i}) + } + return nil + })) + // Multiple reporters are fine. + g.Go(c.Report(func(report func(v val)) error { + for i := range 3 { + report(val{"odd", 2*i + 1}) + } + // An error from a reporter is propagated like any other task error. + return errors.New("no bueno") + })) + err := g.Wait() if err == nil || err.Error() != "no bueno" { log.Fatalf("Unexpected error: %v", err) } diff --git a/taskgroup.go b/taskgroup.go index 3cfbf8a..19c0afc 100644 --- a/taskgroup.go +++ b/taskgroup.go @@ -55,8 +55,8 @@ func (g *Group) activate() { // manipulate local data structures without additional locking. func New(ef ErrorFunc) *Group { return &Group{onError: ef} } -// Go runs task in a new goroutine in g, and returns g to permit chaining. -func (g *Group) Go(task Task) *Group { +// Go runs task in a new goroutine in g. +func (g *Group) Go(task Task) { g.wg.Add(1) if g.active.Load() == 0 { g.activate() @@ -67,14 +67,13 @@ func (g *Group) Go(task Task) *Group { g.handleError(err) } }() - return g } // Run runs task in a new goroutine in g, and returns g to permit chaining. // This is shorthand for: // // g.Go(taskgroup.NoError(task)) -func (g *Group) Run(task func()) *Group { return g.Go(NoError(task)) } +func (g *Group) Run(task func()) { g.Go(NoError(task)) } func (g *Group) handleError(err error) { g.μ.Lock() diff --git a/taskgroup_test.go b/taskgroup_test.go index 24ec1c0..91df2af 100644 --- a/taskgroup_test.go +++ b/taskgroup_test.go @@ -30,7 +30,8 @@ func TestBasic(t *testing.T) { t.Logf("Group value is %d bytes", reflect.TypeOf((*taskgroup.Group)(nil)).Elem().Size()) // Verify that the group works at all. - g := taskgroup.New(nil).Go(busyWork(25, nil)) + g := taskgroup.New(nil) + g.Go(busyWork(25, nil)) if err := g.Wait(); err != nil { t.Errorf("Unexpected task error: %v", err) } @@ -61,7 +62,8 @@ func TestErrorPropagation(t *testing.T) { defer leaktest.Check(t)() var errBogus = errors.New("bogus") - g := taskgroup.New(nil).Go(func() error { return errBogus }) + var g taskgroup.Group + g.Go(func() error { return errBogus }) if err := g.Wait(); err != errBogus { t.Errorf("Wait: got error %v, wanted %v", err, errBogus) } @@ -152,7 +154,8 @@ func TestCapacity(t *testing.T) { func TestRegression(t *testing.T) { t.Run("WaitRace", func(t *testing.T) { ready := make(chan struct{}) - g := taskgroup.New(nil).Go(func() error { + g := taskgroup.New(nil) + g.Go(func() error { <-ready return nil }) @@ -205,7 +208,8 @@ func TestSingleTask(t *testing.T) { return <-release }) - g := taskgroup.New(nil).Run(func() { + g := taskgroup.New(nil) + g.Run(func() { if err := s.Wait(); err != sentinel { t.Errorf("Background Wait: got %v, want %v", err, sentinel) } @@ -309,7 +313,8 @@ func TestCollector_Report(t *testing.T) { var sum int c := taskgroup.Collect(func(v int) { sum += v }) - g := taskgroup.New(nil).Go(c.Report(func(report func(v int)) error { + var g taskgroup.Group + g.Go(c.Report(func(report func(v int)) error { for _, v := range rand.Perm(10) { report(v) }