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 /= (division assignment) operator is a compound assignment operator in Go that divides the current value of the left-hand operand by the value of the right-hand operand, assigning the computed quotient back to the left-hand operand.
x /= y
This operation is logically equivalent to the expanded assignment x = x / (y), but enforces two critical semantic rules:
  1. Single Evaluation: The left-hand operand x is evaluated exactly once. If the left-hand operand contains a function call or side effect (e.g., slice[i()] /= y), the function i() is executed only once. In the expanded form slice[i()] = slice[i()] / (y), i() would be executed twice.
  2. Operator Precedence: The right-hand operand y is evaluated as a fully parenthesized expression. For example, x /= a + b divides x by the sum (a + b). The expanded form without parentheses, x = x / a + b, would incorrectly divide x by a and then add b.

Technical Characteristics

Type Constraints The operator requires both operands to resolve to the same numeric type (integer, floating-point, or complex). Because Go does not perform implicit type coercion between distinct variable types, attempting to use /= with mismatched typed variables (e.g., an int left operand and a float64 right operand) results in a compile-time error. However, if the right-hand operand is an untyped constant, it is implicitly converted to the type of the left-hand operand, provided the constant is representable by that type. Integer Evaluation When applied to integer types (int, int8, uint32, etc.), the underlying division operation truncates the fractional part towards zero. The assigned result is always an integer. Floating-Point Evaluation When applied to floating-point types (float32, float64), the operation retains the fractional quotient and adheres to IEEE 754 standard arithmetic. Division by Zero Behavior
  • Constant Zero: Dividing by a constant zero (both integer 0 and floating-point 0.0) is a compile-time error (division by zero).
  • Runtime Integer Zero: If the right-hand operand is an integer variable that evaluates to 0 at runtime, the operation triggers a runtime panic (panic: runtime error: integer divide by zero).
  • Runtime Floating-Point Zero: If the right-hand operand is a floating-point variable that evaluates to 0.0 at runtime, the operation does not panic. Instead, it assigns positive infinity (+Inf), negative infinity (-Inf), or Not-a-Number (NaN) to the left operand, depending on whether the left operand is positive, negative, or zero, respectively.

Syntax Visualization

// Integer division assignment (truncation towards zero)
var a int = 14
a /= 3 
// a is now 4

// Floating-point division assignment with an untyped constant
var b float64 = 14.0
b /= 4 
// Untyped integer constant 4 is implicitly converted to float64
// b is now 3.5

// Precedence and evaluation demonstration
var c float64 = 10.0
c /= 2.0 + 3.0 
// Equivalent to c = c / (2.0 + 3.0). c is now 2.0

// Complex number division assignment
var d complex128 = 10 + 10i
d /= 2 + 0i 
// d is now 5 + 5i

// Floating-point division by zero (evaluated at runtime)
var e float64 = 5.0
var z float64 = 0.0
e /= z 
// e is now +Inf
Master Go with Deep Grasping Methodology!Learn More