I am a bit confused about multiple assignments in Go, e.g. varA, varB = varC, varD
.
My question is, is such an operation atomic (not on the HW layer of course, but from the programmer's point of view)?
Personally, I expect it to be atomic, so this expression a, b = b, a
just swap the values. I tried it in the Go playground and yes, it worked exactly like that.
On the other hand, the Golang specs states:
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.
which I understand as a
is assigned b
's value, then b
is assigned a
's value (which is now b
), so in the end, both a
and b
equals b
's original value.
Are the specs wrong or do I misunderstand it?