The Go Programming Language Specification
Assignments
The assignment proceeds in two phases. First, the operands of index
expressions and pointer indirections (including implicit pointer
indirections in selectors) on the left and the expressions on the
right are all evaluated in the usual order. Second, the assignments
are carried out in left-to-right order.
The usual example to illustrate multiple assignments is a swap. For example,
package main
import "fmt"
func main() {
{
i, j := 7, 42
fmt.Println(i, j)
// swap i and j - implicit temporaries
i, j = j, i
fmt.Println(i, j)
}
fmt.Println()
{
i, j := 7, 42
fmt.Println(i, j)
// swap i and j - explicit temporaries
ti, tj := i, j
i, j = tj, ti
fmt.Println(i, j)
}
}
Playground: https://play.golang.org/p/HcD9zq_7tqQ
Output:
7 42
42 7
7 42
42 7
The one statement multiple assignment, which uses implicit temporary variables, is equivalent to (a shorthand for) the two multiple assignment statements, which use explicit temporary variables.
Your fibonacci example translates, with explicit order and temporary variables, to:
package main
import "fmt"
func fibonacciMultiple() func() int {
current, next := 0, 1
return func() int {
current, next = next, current+next
return current
}
}
func fibonacciSingle() func() int {
current, next := 0, 1
return func() int {
// current, next = next, current+next
// first phase, evaluation, left-to-right
t1 := next
t2 := current + next
// second phase, assignmemt, left-to-right
current = t1
next = t2
return current
}
}
func main() {
m := fibonacciMultiple()
fmt.Println(m(), m(), m(), m(), m(), m())
s := fibonacciSingle()
fmt.Println(s(), s(), s(), s(), s(), s())
}
Playground: https://play.golang.org/p/XFq-0wyNke9
Output:
1 1 2 3 5 8
1 1 2 3 5 8