Skip to content

Instantly share code, notes, and snippets.

@matheusd
Created October 20, 2025 14:49
Show Gist options
  • Select an option

  • Save matheusd/c480f61670fc27deabb2a6e193fc344d to your computer and use it in GitHub Desktop.

Select an option

Save matheusd/c480f61670fc27deabb2a6e193fc344d to your computer and use it in GitHub Desktop.
type runner struct {}
func (r *runner) run(ctx context.Context) error {
g, gctx := errgroup.WithContext(ctx)
// loads of goroutines
g.Go(func() error {
// something something, then
return context.Cause(gctx)
})
return g.Wait()
}
func runStuff(ctx context.Context) error {
r := &runner{}
ctx, cancel := context.CancelWithCause(ctx)
errChan := make(chan error)
errGuardError := errors.New("this is the error i want")
go func () { errChan <- r.run(ctx) }()
go func () {
time.Sleep(time.Second)
cancel(errGuardError)
}()
err := <-errChan
if errors.Is(err, errGuardError) {
// ????
err = nil
}
return err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment