Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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.
operand1 += operand2

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:
var count int = 10
count += 5 
// Evaluates as: count = count + (5)
// Result: 15
Order of Operations Preservation:
var x float64 = 1.5
var a float64 = 2.0
var b float64 = 3.0

x += a + b
// Evaluates as: x = x + (a + b)
// The parentheses prevent mathematically different results in floating-point arithmetic.
String Concatenation:
var prefix string = "sys_"
prefix += "admin" 
// Evaluates as: prefix = prefix + ("admin")
// Result: "sys_admin"
Strict Typing Enforcement:
var a int32 = 10
var b int64 = 5

// a += b 
// Compiler Error: invalid operation: a += b (mismatched types int32 and int64)

a += int32(b) // Valid: Explicit type conversion satisfies type equivalence

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.
// The function computeIndex() is called exactly once.
// If this were written as slice[computeIndex()] = slice[computeIndex()] + (1), 
// the function would be executed twice.
slice[computeIndex()] += 1
Master Go with Deep Grasping Methodology!Learn More