Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does multiple assignment have order?

Tags:

go

I know golang supports multiple assignment, for instance,

a, b = c, d

I want to know if the assignment following the left->right order? For instance, if I play with trees:

parent, child = child, child.child

Does it guarantee both parent and child are assigned one level deeper in the tree?

like image 883
WhatABeautifulWorld Avatar asked Aug 05 '17 22:08

WhatABeautifulWorld


People also ask

What is multiple assignment?

multiple assignment A form of assignment statement in which the same value is given to two or more variables. For example, in Algol, a := b := c := 0. sets a, b, c to zero. A Dictionary of Computing. "multiple assignment ."

How do I assign multiple values to multiple variables?

You can assign the same value to multiple variables by using = consecutively. This is useful, for example, when initializing multiple variables to the same value. It is also possible to assign another value into one after assigning the same value.

Is multiple assignment possible in C?

MultipleAssignment is a language property of being able to assign one value to more than one variables. It usually looks like the following: a = b = c = d = 0 // assigns all variables to 0.


1 Answers

Yes. From the language spec:

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.

So in your example, child and child.child will be evaluated first, then assigned to parent and child respectively.

like image 182
Flimzy Avatar answered Oct 30 '22 22:10

Flimzy