On Tue, 1 May 2018 04:11:03 -0700 (PDT)
Hugh Fisher <[email protected]> wrote:
> Another observation from this novice Go programmer: I'm puzzled why
> there's no while statement.
Because 'for' keeps condition where it belongs (at top);
If you want `do..while` as in C, Go offers more readable construct
with naked for:
n := 5
for { // do
if !(n < 10) { // do while
break
}
// body done at least once
println(n)
n++
}
Version without !(condition) adds else:
for { // do
if n < 10 { // while,
} else {
break
}
// body done at least once
println(n)
n++
}
If you do want mix it with full fledged for, you can:
for do1, n := true, 5; n < 10 || do1; do1, n = false, n+1 {
// do body at least once
println(n)
}
Though last example is not in Go's "readability first" spirit.
https://play.golang.org/p/8AKokpQWxkr
> Hugh Fisher
>
--
Wojciech S. Czarnecki
<< ^oo^ >> OHIR-RIPE
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.