Skip to main content
The -= (subtraction assignment) operator is a compound assignment operator that subtracts the value of the right operand from the left operand and assigns the resulting difference back to the left operand.
Semantically, this operation is equivalent to the expanded assignment form, with the exception that the left operand is evaluated exactly once:

Technical Mechanics and Constraints

Assignable Expressions The left operand must be an addressable expression or a map index expression. Valid left-hand side operands include variables, pointer indirections, array elements, slice elements, and map elements. It cannot be a literal, a constant, or the result of a function call returning a value. While map index expressions (e.g., m[key]) are explicitly not addressable in Go, the language specification carves them out as a valid exception for assignment operations. Type Compatibility Go enforces strict type safety for compound assignments.
  • Both operands must resolve to identical numeric types (e.g., int, float64, complex128).
  • If the right operand is an untyped constant, it must be implicitly convertible to the declared type of the left operand without truncation or overflow.
  • The operator does not support strings, booleans, or custom structs.
Evaluation Semantics Because the left operand is evaluated only once, using -= is safer and more efficient when the left operand contains a function call or a complex pointer dereference.
Arithmetic Underflow For fixed-size integer types (e.g., uint8, int32), the -= operator is subject to standard two’s complement arithmetic wrap-around. If the subtraction results in a value below the minimum representable bound of the left operand’s type, it will silently underflow. The Go compiler and runtime do not trigger a panic on integer underflow.
Tired of Poor Go Skills? Fix That With Deep Grasping!Learn More