Junior — Middle
Kas on veadide rühm ja kuidas seda kasutatakse jälgimisel või erandite töötlemisel?
sobes.tech AI
Vastus AI-lt
Error group is a structure that allows combining multiple errors into one, making it easier to handle and return them. In Go, this is useful when you need to perform several operations in parallel and collect all errors that occur into a single object.
Example of using the error group from the package golang.org/x/sync/errgroup:
import (
"fmt"
"golang.org/x/sync/errgroup"
)
func process() error {
var g errgroup.Group
g.Go(func() error {
// first task
return nil
})
g.Go(func() error {
// second task
return fmt.Errorf("error in second task")
})
// Wait for all tasks to complete and get the first error if any
if err := g.Wait(); err != nil {
return err
}
return nil
}
This way, the error group helps conveniently aggregate errors from multiple goroutines and simplifies their handling.