Search code examples
gogoroutine

Golang error group over function with custom signature


I have a function, say, func foo(x) error { if x == y ... return err} that I would like to execute in several go routines and aggregate the errors (preferably using an error group as with the common example below), but not sure if that's possible?

   import "golang.org/x/sync/errgroup"

   var g errgroup.Group

   g.Go(func() error {
      resp, err := http.Get(url)
      if err == nil {
         resp.Body.Close()
      }
      return err
   })

Solution

  • Answer as proposed by @mkopriva.

    var g errgroup.Group
    var x interface{}
    
    g.Go(func() error {
       return foo(x)
    })
    

    Simples.