Created
October 20, 2025 14:49
-
-
Save matheusd/c480f61670fc27deabb2a6e193fc344d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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