What an augmented assignment is
Changing a variable based on its own value is one of the most common things a program does. Go evaluates the right side of an assignment first, then stores the result, so this line adds 5 to myVar:
myVar = myVar + 5
Because the pattern is so common, Go provides a shorter form that does the arithmetic and the assignment in one step. It is called an augmented assignment (or compound assignment):
myVar := 10
myVar += 5
fmt.Println(myVar) // 15
myVar += 5 means exactly myVar = myVar + 5. The variable name is written once, which is shorter and avoids typos when the name is long.
One operator for each arithmetic operation
| Operator | Short for | Example with myVar starting at... | Result |
|---|---|---|---|
+= | myVar = myVar + n | 10, then myVar += 5 | 15 |
-= | myVar = myVar - n | 10, then myVar -= 5 | 5 |
*= | myVar = myVar * n | 3, then myVar *= 5 | 15 |
/= | myVar = myVar / n | 20, then myVar /= 4 | 5 |
%= | myVar = myVar % n | 11, then myVar %= 3 | 2 |
Every example is a single line applied to a variable that already exists:
myVar := 20
myVar /= 4
fmt.Println(myVar) // 5
myVar = 11
myVar %= 3
fmt.Println(myVar) // 2
Go details
- The type does not change.
number := 18creates anint, and every augmented assignment keeps it anint. With two integers,/=is integer division: ifmis 26, thenm /= 5leaves 5, not 5.2. - The right side is computed in full first.
x *= 3 + 4meansx = x * (3 + 4). Forxequal to 2, that is 14, whilex = x * 3 + 4would give 10. - It is a statement. You can't use
x += 1inside another expression, such asfmt.Println(x += 1). - The variable must exist.
:=declares a new variable. Later changes use=or the augmented forms; writing:=again for the same name in the same scope is an error. - It works on other types.
+=also joins strings, sos := "go"followed bys += "lang"makes"golang", and it works onfloat64too. - Overflow wraps. For sized integers, the value wraps around silently: a
uint8holding 250 after+= 10becomes 4.
When to use them
Use an augmented assignment whenever the new value of a variable depends on its old value: a running total (total += price), a countdown (lives -= 1), a repeated scaling (size *= 2) or a value that cycles inside a range (index %= length). Reading total += price aloud, "add price to total", matches what the program does, which makes it easy to read. Adding or subtracting exactly 1 has dedicated operators, ++ and --, which have a lesson of their own.
The lesson's exercise
The exercise is a seven-step drill in which one variable, number, is changed with each operator in turn. Track its value as you go:
- Create the variable.
number := 18declares it and infersint. number += 8adds 8, so it becomes 26.number -= 1subtracts 1, giving 25.number /= 5divides by 5. 25 divides evenly, so the result is 5.number += 1raises it to 6.number *= 3multiplies by 3 to get 18.number -= 18subtracts the current value from itself and leaves 0.
number := 18
number += 8
number -= 1
number /= 5
number += 1
number *= 3
number -= 18
Adding a fmt.Println(number) after each line prints 26, 25, 5, 6, 18 and 0. That habit is the best way to check your reasoning: print after every step until you trust the sequence.
Common mistakes and gotchas
- Writing the operator backwards.
=+is parsed as an assignment of a positive number:x =+ 5isx = +5, not an addition. The operator character comes first:+=. - Spaces inside the operator.
+ =is not valid Go. Write both characters together. - Expecting decimals from
/=. Anintvariable stays anint, and the fraction is dropped. - Declaring with
:=twice. The secondnumber := 5in the same scope fails to compile. Usenumber = 5to change the value. - Using the operator on a variable that doesn't exist yet.
total += 5beforetotalis declared is an undefined-name error.
Key takeaways
x op= yis shorthand forx = x op y, for+,-,*,/and%.- The right side is evaluated completely before the operation is applied.
- The variable keeps its type, so integer variables use integer division.
- Declare with
:=once, then change with=or the augmented operators.