9 karma · joined Nov 2, 2016 (10y ago) · 6 submissions
comment 3y ago · link · parent
var errClosed = errors.New("closed")
func doAThing[T any](ctx context.Context, c chan T) (T, error) {
// the generics are not necessary, but they're also not awful for
// DRYing up this type of a select.
select {
case <-ctx.Done():
var empty T
return empty, fmt.Errorf("could not doAThing: %w", ctx.Err())
case item, ok := <-c:
if !ok {
var empty T
return empty, fmt.Errorf("channel of type %T is done: %w", empty, errClosed)
}
return item, nil
}
}
// ... and later...
for {
item, err := doAThing(ctx, channel)
if err != nil {
break
}
// code goes here
}
I dislike labeled loops, naked returns, and else statements, so this is the pattern I use.comment 8y ago · link · parent
There's a table in the above link, as well as a few more details
comment 9y ago · link · parent
They may not have the greatest wifi range in the world, but I haven't found a router to beat them at stability.