Start learning free

LESSON 25 OF 338 · CALCULATIONS WITH NUMBERS

Augmented Assignment Operators in Go: +=, -=, *=, /= and %=

Lesson Read the full explanation

In programming, it is common to use assignments to modify the contents of a variable.

Everything to the right of the equals sign (=) is evaluated first, so we can say:

myVar = myVar + 5

to add 5 to myVar.

Since this is such a common pattern, there are operators which do both a mathematical operation and assignment in one step.

One such operator is the += operator:

myVar := 10
myVar += 5
fmt.Println(myVar) // Output: 15

This code would print 15 to the console. We call myVar += 5 an augmented assignment.

There is one such operator for every other arithmetic operator we've learned:

For subtraction: -=

myVar := 10
myVar -= 5 // short for myVar = myVar - 5
fmt.Println(myVar) // Output: 5

For multiplication: *=

myVar := 3
myVar *= 5 // short for myVar = myVar * 5
fmt.Println(myVar) // Output: 15

For division: /=

myVar := 20
myVar /= 4 // short for myVar = myVar / 4
fmt.Println(myVar) // Output: 5

For remainder: %=

myVar := 11
myVar %= 3 // short for myVar = myVar % 3
fmt.Println(myVar) // Output: 2

Assignment
Follow the Coding Tutorial and let's do some augmented assignments.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 7
    Ctrl + Enter to check

    GO · CALCULATIONS WITH NUMBERS

    Augmented Assignment Operators in Go, explained

    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

    OperatorShort forExample with myVar starting at...Result
    +=myVar = myVar + n10, then myVar += 515
    -=myVar = myVar - n10, then myVar -= 55
    *=myVar = myVar * n3, then myVar *= 515
    /=myVar = myVar / n20, then myVar /= 45
    %=myVar = myVar % n11, then myVar %= 32

    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 := 18 creates an int, and every augmented assignment keeps it an int. With two integers, /= is integer division: if m is 26, then m /= 5 leaves 5, not 5.2.
    • The right side is computed in full first. x *= 3 + 4 means x = x * (3 + 4). For x equal to 2, that is 14, while x = x * 3 + 4 would give 10.
    • It is a statement. You can't use x += 1 inside another expression, such as fmt.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, so s := "go" followed by s += "lang" makes "golang", and it works on float64 too.
    • Overflow wraps. For sized integers, the value wraps around silently: a uint8 holding 250 after += 10 becomes 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:

    1. Create the variable. number := 18 declares it and infers int.
    2. number += 8 adds 8, so it becomes 26.
    3. number -= 1 subtracts 1, giving 25.
    4. number /= 5 divides by 5. 25 divides evenly, so the result is 5.
    5. number += 1 raises it to 6.
    6. number *= 3 multiplies by 3 to get 18.
    7. number -= 18 subtracts 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 =+ 5 is x = +5, not an addition. The operator character comes first: +=.
    • Spaces inside the operator. + = is not valid Go. Write both characters together.
    • Expecting decimals from /=. An int variable stays an int, and the fraction is dropped.
    • Declaring with := twice. The second number := 5 in the same scope fails to compile. Use number = 5 to change the value.
    • Using the operator on a variable that doesn't exist yet. total += 5 before total is declared is an undefined-name error.

    Key takeaways

    • x op= y is shorthand for x = 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.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free