Reputation: 8783
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!
Upvotes: 3
Views: 5809
Reputation: 19799
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
}
}
Upvotes: 8