Closed
Description
I don't want to speak for other developers, but a very quick and dirty (yet effective) construct that I typically use for resilient code is the following retry pattern:
for {
if err := try_something(); err == nil {
break
}
fmt.Println("something went wrong, trying again in 5s", "err", err.Error())
time.Sleep(5 * time.Second)
}
For a brief moment, I thought I could brilliantly shorten this to the following:
for err := try_something; err != nil; {
fmt.Println("something went wrong, trying again in 5s", "err", err.Error())
time.Sleep(5 * time.Second)
}
only to face palm myself about a minute later for neglecting the fundamentals of a for
loop.
That being said, I believe that it would be a perfect addition to Go 2.0 (as this would be a language breaking change) to add support for such a construct by allowing the for
loop to only contain a single semicolon, that is:
for err := try_something; err != nil {
fmt.Println("something went wrong, trying again in 5s", "err", err.Error())
time.Sleep(5 * time.Second)
}
Where the first clause is executed before every iteration of the loop and the second is the condition that breaks if true, else executes the block.
Thoughts?