I'm looping over state changes where the change & condition are defined in a function and was hoping to write a for statement where I only declare the function call once.
This code works, but repeating message, flag = a.foo()
feels messy. Is there a more effective way to write a for loop when the initial statement is the same as the post statement?
func (a *Example) foo() (string, bool) {
// Function affects state of a
// returns message on each step and flag if condition reached
}
func main() {
// Step through each state change until flag is triggered
for message, flag := a.foo(); flag; message, flag = a.foo() {
fmt.Printf("%s
", message)
}
}
I was hoping that there would be some kind of equivalent to how nicely it fits in to an if statement like this:
if message, flag := a.foo(); !flag {
fmt.Printf("%s
", message)
}
In fact, I was rather hoping I could just change that if to a for but, alas, expected ';'. found '{'
I may well be asking too much, but if there is an answer I'd love to hear it. Thanks