Introduction
Assignment operators help you store or update values in variables easily.
variable = value variable += value variable -= value variable *= value variable /= value variable %= value
x := 10 x += 5 // x is now 15
y := 20 y -= 4 // y is now 16
z := 3 z *= 7 // z is now 21
a := 15 a /= 3 // a is now 5
package main import "fmt" func main() { var score int = 10 fmt.Println("Initial score:", score) score += 5 fmt.Println("After adding 5:", score) score -= 3 fmt.Println("After subtracting 3:", score) score *= 2 fmt.Println("After multiplying by 2:", score) score /= 4 fmt.Println("After dividing by 4:", score) score %= 3 fmt.Println("After modulo 3:", score) }
+= operator do in Go?+= operator adds the right-hand side value to the left-hand side variable.+= means add and assign [OK]x by 5 and store the result back in x?*=, used as x *= 5.*= for multiply and assign [OK]package main
import "fmt"
func main() {
a := 10
a += 5
a -= 3
fmt.Println(a)
}a = 10, after a += 5, a becomes 15.a -= 3 subtracts 3, so a becomes 12.package main
import "fmt"
func main() {
var x int = 10
x + = 5
fmt.Println(x)
}+= must not have spaces; + = is invalid syntax.x is declared correctly, semicolons are optional in Go, and the fmt package is imported.+ = has a space and is invalid -> Option Atotal?
package main
import "fmt"
func main() {
total := 20
total /= 4
total *= 3
total %= 5
fmt.Println(total)
}total = 20, after total /= 4, total becomes 5 (integer division).total *= 3 makes total 15.total %= 5 calculates remainder of 15 divided by 5, which is 0.Pass the test and this pattern moves unknown