What's wrong with
   for ;; time.Sleep(delay) { ... }
?

This technique is as old as the hills. Or at least as old as C for loops.

-rob



On Wed, Jan 12, 2022 at 9:02 PM Tobias Klausmann
<klaus...@schwarzvogel.de> wrote:
>
> 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
>
> --
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/Yd6nL/ScO9gO0TgB%40skade.schwarzvogel.de.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOXNBZRTUzJEAr%3DsFCyidqb11y6BP49c8RFkGTbykRwYLo9e%3Dg%40mail.gmail.com.

Reply via email to