Shortcut for declaration and initialization in go
var a, b, c = 1 , 2, 3
Equivalent to the following declaration and initialization method (as per specification )
But I do not get the answer to the problem found in the following code:
package main import "fmt" func main() { var a int = 0 var b int = 1 fmt.Println("init a ",a) fmt.Println("init b ",b) a, b = b, a+b fmt.Println("printing a after `a, b = b, a+b`",a) fmt.Println("printing b after `a, b = b, a+b`",b) }
The conclusion should be:
printing a after 'a, b = b, a+b' 1 printing b after 'a, b = b, a+b' 2
Since the value of b is estimated using a + b ie 1+1 = 2. But its value is 1.
Here are links to playgrounds as a working code where you can watch the difference.
I know that I am missing something to understand, basically, how an expression of shorthand is evaluated, especially when the same variable is involved in the expression.
But where should the relevant documentation be. Can anyone help with this?
source share