WhatABeautifulWorld
WhatABeautifulWorld

Reputation: 3388

Does multiple assignment have order?

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?

Upvotes: 12

Views: 1768

Answers (1)

Jonathan Hall
Jonathan Hall

Reputation: 79546

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.

Upvotes: 21

Related Questions