Do-while loop in 2 different ways

yourbasic.org/golang

There is no do-while loop in Go. To emulate the C/Java code

do {
    work();
} while (condition);

you may use one of these two alternatives:

for ok := true; ok; ok = condition {
    work()
}
for {
    work()
    if !condition {
        break
    }
}

Further reading

For loops in a nutshell

Share this page: