Flash cards
Review the key moves
1/4
Core idea
What is the main idea behind Go Assignment Operators?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
1Quick choice
Which statement best captures the main point of this lesson?
2Fill blank
Complete the missing token from the example code.
___ main3Order
Put the learning moves in the order that makes the concept easiest to apply.
In the example below, we use the assignment operator ( = ) to assign the value 10 to a variable called x :
Assignment operators are used to assign values to variables.
Assignment Operators
Assignment Operators
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator ( = ) to assign the value 10 to a variable called x :
Example
package main
import ("fmt")
func main() {
var x = 10
fmt.Println(x)
}The addition assignment operator ( += ) adds a value to a variable:
Example
package main
import ("fmt")
func main() {
var x = 10
x +=5
fmt.Println(x)
}A list of all assignment operators
| Operator | Example | Same As | ||
|---|---|---|---|---|
| = | x = 5 | x = 5 | ||
| += | x += 3 | x = x + 3 | ||
| -= | x -= 3 | x = x - 3 | ||
| *= | x *= 3 | x = x * 3 | ||
| /= | x /= 3 | x = x / 3 | ||
| %= | x %= 3 | x = x % 3 | ||
| &= | x &= 3 | x = x & 3 | ||
| = | x | = 3 | x = x | 3 |
| ^= | x ^= 3 | x = x ^ 3 | ||
| >>= | x >>= 3 | x = x >> 3 | ||
| <<= | x <<= 3 | x = x << 3 |