package builtin import ( "context" ) // runWithContext runs fn and returns its result, or ctx.Err() if ctx ends // first; fn keeps running in the background. fn is not started if ctx is // already done. func runWithContext(ctx context.Context, fn func() error) error { if err := ctx.Err(); err != nil { return err } done := make(chan error, 1) go func() { done <- fn() }() select { case err := <-done: return err case <-ctx.Done(): return ctx.Err() } }