by lalaithion · 3y ago · 0 comments
for item := range channel {
// code goes here
}
becomes for {
var item Item
var ok bool
select {
case <-ctx.Done():
break
case item, ok = <-channel:
if !ok {
break
}
}
// code goes here
}
(If you have a solution to this pattern, please do share! I would _love_ a better way.)That code is written like that because it's the only way to write code like that. You have a channel producing items, it will produce items for as long as the channel is open. You have a context to propagate cancellation.
You code gets bigger but it doesn't explode in size as this boilerplate is static in size regardless of what computation you're doing with those items.
You can improve that code by the way by fixing a bug and reducing the lines.
L:
for {
select {
case <-ctx.Done():
break L
case item, ok := <-channel:
if !ok {
break L
}
// code goes here
}
}
If you think there's a way to write that code any more concise than that, then there isn't. I don't see how this is an issue. The fact that there's pretty much one way to write this code makes it so much easier to parse when reading code that uses this pattern. To me, that's great. 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. func streamingApply[T any](ch channel[T], fn func(T)){
for {
select {
case <-ctx.Done():
break
case item, ok = <-ch:
if !ok {
break
}
fn(item)
}
}
}
For everyone that's complained about generics being introduced to go...It really does help clean up this type of repetition.That being said, my experience is that anything moderately sophisticated requires the use of for/select. In my opinion, that's fine, because select gives you so much power the somewhat more complex form is worth it.
Now if you cant use another language, change project. Go is not perl nor python, it's a sophisticated C. Use a sophisticated C for the wrong project / product and you'll want to kill yourself.
for item := range channel {
_, err := process(ctx, item)
if err != nil {
return err
}
}Thank you!