Tobias Klausmann
unread,Jan 12, 2022, 5:03:01 AM1/12/22Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golan...@googlegroups.com
Hi!
Often with tools that poll something, you get code of this form:
```
for {
r, err := doSomeCall()
if err != nil {
log.Printf("Some error:", err)
continue
}
s, err := doSomeOtherCall(r)
if err != nil {
log.Printf("Some other error:", err)
continue
}
}
```
This works nice and dandy, except that it of course runs as fast/hard as
it can, so usually, one would have a `time.Sleep()` or something like it
at the end of the `for{}`. Except: now the error handling blocks can't
use `continue` anymore. I can of course make this a nested set of
`if {} else {}` blocks, but beyond two calls, that is very ugly and hard
to understand.
So what is the *idiomatic* way of being able to use `continue` (or
something like it), yet have "always do this" code at the end of the
loop? As I understand it, `defer` only works for ends of functions, not
ends of blocks, and label breaks only work for breaks, obviously.
Best,
Tobias