Skip to main content
The += operator is a compound assignment operator that performs addition (or string concatenation) and assignment in a single operation. According to the Go specification, it evaluates the statement x += y as x = x + (y), with the strict compiler guarantee that the left-hand operand x is evaluated exactly once. The implicit parentheses around the right-hand operand ensure that the order of operations is preserved, which is critical for operations lacking strict associativity, such as floating-point arithmetic.

Mechanics and Type Constraints

Because Go is a statically and strictly typed language, the += operator enforces rigid type safety rules during compilation:
  1. Type Equivalence: operand1 and operand2 must be of the identical type. Go does not perform implicit type coercion between different numeric types (e.g., int32 and int64).
  2. Untyped Constants: If operand2 is an untyped constant, it must be implicitly representable by the type of operand1.
  3. Statement, Not Expression: In Go, assignment operations are statements, not expressions. You cannot assign the result of a += operation to another variable or evaluate it inline (e.g., z := (x += y) is a syntax error).

Supported Data Types

The operator exhibits polymorphic behavior depending on the underlying types of the operands:
  • Numeric Types: Performs standard arithmetic addition. Supported types include all integer variants (signed and unsigned), floating-point numbers (float32, float64), and complex numbers (complex64, complex128).
  • Strings: Performs string concatenation, appending the right operand to the end of the left operand and allocating a new underlying string in memory.

Syntax Visualization

Numeric Addition:
Order of Operations Preservation:
String Concatenation:
Strict Typing Enforcement:

Single Evaluation Guarantee

When the left-hand operand contains an expression (such as a function call, pointer dereference, or map/slice index), Go guarantees that the expression is evaluated only once.
Tired of Poor Go Skills? Fix That With Deep Grasping!Learn More