Comment

Comment by assbuttbuttass

I would write the author's example as follows:

    for ctx.Err() == nil {
        select {
        case <-ctx.Done():
            return nil
        case thing := <-thingCh:
            // Process thing...
        case <-time.After(5*time.Second):
            return errors.New("timeout")
        }
    }
The extra check for ctx.Err before the select statement easily resolves the author's issue.

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton