Sorry for going silent without warning. I took a holiday, and due to a technical snafu, failed to send the announcement, so you’d know not to expect emails from me for a week and a half.
Anyway, I’m back now…
Let’s jump back into our tour through assignment statments, with an explanation of the special assignment/arithmetic operator combinations. You know, those things like +=
, -=
, and <<=
:
Assignment statements
…
An assignment operation
x
op=
y
where op is a binary arithmetic operator is equivalent tox
=
x
op(y)
but evaluatesx
only once. The op=
construct is a single token. In assignment operations, both the left- and right-hand expression lists must contain exactly one single-valued expression, and the left-hand expression must not be the blank identifier.a[i] <<= 2 i &^= 1<<n
Simply put, x = x <op> y
can be shortened to x <op>= y
. But this is limited to single-value expressions. This means if you find yourself in (probably unusual) situation of needing to, say, add a bunch of values at once, you can do this:
x, y, z = x + 1, y + 2, z + 3
But you cannot shorten it to:
x, y, z += 1, 2, 3
And even more devastatingly*, you cannot shorten x, y, z = x + 1, y + 1, z + 1
to:
x, y, z += 1
*I hope the intended sarcastic tone is apparent.
Quotes from The Go Programming Language Specification Language version go1.22 (Feb 6, 2024)