@@ -10,11 +10,11 @@ import (
1010 "sync"
1111)
1212
13- // A Group is a collection of goroutines working on subtasks that are part of
13+ // A group is a collection of goroutines working on subtasks that are part of
1414// the same overall task.
1515//
16- // A zero Group is valid and does not cancel on error.
17- type Group struct {
16+ // A zero group is valid and does not cancel on error.
17+ type group struct {
1818 cancel func ()
1919
2020 wg sync.WaitGroup
@@ -23,23 +23,35 @@ type Group struct {
2323 err error
2424}
2525
26- func NewErrGroup () * Group {
27- return & Group {}
26+ type OptionFunc func (o * group )
27+
28+ func NewErrGroup (of ... OptionFunc ) * group {
29+ o := new (group )
30+ for _ , optionFunc := range of {
31+ optionFunc (o )
32+ }
33+ return o
34+ }
35+
36+ func SetCancel (f func ()) OptionFunc {
37+ return func (o * group ) {
38+ o .cancel = f
39+ }
2840}
2941
3042// WithContext returns a new Group and an associated Context derived from ctx.
3143//
3244// The derived Context is canceled the first time a function passed to Go
3345// returns a non-nil error or the first time Wait returns, whichever occurs
3446// first.
35- func WithContext (ctx context.Context ) (* Group , context.Context ) {
47+ func WithContext (ctx context.Context ) (* group , context.Context ) {
3648 ctx , cancel := context .WithCancel (ctx )
37- return & Group {cancel : cancel }, ctx
49+ return & group {cancel : cancel }, ctx
3850}
3951
4052// Wait blocks until all function calls from the Go method have returned, then
4153// returns the first non-nil error (if any) from them.
42- func (g * Group ) Wait () error {
54+ func (g * group ) Wait () error {
4355 g .wg .Wait ()
4456 if g .cancel != nil {
4557 g .cancel ()
@@ -51,7 +63,7 @@ func (g *Group) Wait() error {
5163//
5264// The first call to return a non-nil error cancels the group; its error will be
5365// returned by Wait.
54- func (g * Group ) Go (f func () error ) {
66+ func (g * group ) Go (f func () error ) {
5567 g .wg .Add (1 )
5668
5769 go func () {
0 commit comments