I'm back!

April 19, 2024

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 to x = x op (y) but evaluates x 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)


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Assignment order

We’ve already talked about the order of evaluation in Go. And while that’s perfectly relevant here, it’s only one piece of the puzzle when it comes to ordering with assignment operations: Assignment statements … 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.


Tuple assignments

Yesterday (as well as in earlier emails) I mentioned multiple-assignments in a single statement. Today we see how they’re defined to work. As such, there’s not really an new material here, but we’ll cover it just the same. Assignment statements … A tuple assignment assigns the individual elements of a multi-valued operation to a list of variables. There are two forms. In the first, the right hand operand is a single multi-valued expression such as a function call, a channel or map operation, or a type assertion.


Assignment statements

Assignment statements An assignment replaces the current value stored in a variable with a new value specified by an expression. Assignments should be familiar to you. They’re pretty fundamental to almost all programming languages. But there are a few aspects to Go’s assignments that are worth calling out. … An assignment statement may assign a single value to a single variable, or multiple values to a matching number of variables.

Get daily content like this in your inbox!

Subscribe