I am trying to write a for loop in Go with multiple variables.
Coming from the javascript world, I'd like to achieve something like this:
var i = 10;
var b = 2;
for (var a = b; i; i /= 2, b *= b ) {
// some code
}
I've tried a 'raw translation' like this:
i, b := 10, 2
for a := b; i; i /= 2, b *= b {
// some code
}
But it doesn't work. What is the proper syntax?
Many thanks!
In Go, you can do multiple variable assignment in a loop like so.
package main
func main() {
var (
i = 10
b = 2
)
for a := b; i != 0; i, b = i/2, b*b {
// some code
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With