crand icon indicating copy to clipboard operation
crand copied to clipboard

For is Go's while loop

Open djherbis opened this issue 8 years ago • 3 comments

https://tour.golang.org/flowcontrol/3

Looks like you use for do := true; do; do = condition {}.

You can simplify this to for condition {}

djherbis avatar May 04 '16 02:05 djherbis

I want to check the condition after each iteration. I couldn't find a more idiomatic way to accomplish that.

ammario avatar May 04 '16 02:05 ammario

Ah, I meant to say you could do:

Rand(buf)
for cond { 
    Rand(buf)
}

Feels a little more obvious about what the loop is doing.

Why not do something like what math/rand does:

https://golang.org/src/math/rand/rand.go?s=2478:2514#L69

djherbis avatar May 04 '16 03:05 djherbis

seems like the loop you described is exactly what math.Rand does, I'll refactor next time I have a little bit of time. Thanks.

ammario avatar May 04 '16 03:05 ammario