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 in Go is a binary arithmetic operator that computes the quotient of two numeric operands. Its specific execution behavior, including precision, truncation, and error handling, is strictly determined by the resolved data types of the operands.
quotient := dividend / divisor

Integer Division

When both operands are of an integer type (e.g., int, int64, uint8), the / operator performs integer division. The result is always an integer of the same type. Go implements truncation towards zero, meaning any fractional component of the mathematical quotient is discarded.
var a, b int = 7, 3
var c int = a / b // c == 2

var x, y int = -7, 3
var z int = x / y // z == -2
Note: Attempting integer division by a constant zero (e.g., x / 0) results in a compile-time error (invalid operation: division by zero). If the divisor is a variable or non-constant expression that evaluates to zero at runtime (e.g., x / y where y == 0), it triggers a runtime panic.

Floating-Point Division

When the operands are of a floating-point type (float32 or float64), the / operator performs standard IEEE-754 division, preserving fractional precision.
var a, b float64 = 7.0, 3.0
var c float64 = a / b // c == 2.3333333333333335
In Go, dividing by a constant zero (e.g., 1.0 / 0.0) is always a compile-time error, regardless of the numeric type. However, at runtime, floating-point division by a variable that evaluates to zero does not trigger a panic. Instead, it yields special IEEE-754 values depending on the signs of both the dividend and the divisor (Go strictly adheres to IEEE-754, which distinguishes between 0.0 and -0.0):
var pos, neg, zero float64 = 1.0, -1.0, 0.0

// Runtime evaluation of division by zero
var r1 float64 = pos / zero  // +Inf (Positive Infinity: same sign)
var r2 float64 = neg / zero  // -Inf (Negative Infinity: opposite signs)
var r3 float64 = zero / zero // NaN  (Not a Number: both are zero)

Complex Number Division

The operator natively supports complex number types (complex64 and complex128), executing standard algebraic division for complex numbers.
var c1, c2 complex128 = complex(2, 4), complex(1, 1)
var c3 complex128 = c1 / c2 // c3 == (3+1i)

Type Constraints

Go’s strict typing system mandates that both operands must be of the exact same type. Go does not perform implicit type coercion (widening or narrowing). Dividing an int by a float64 will result in a compile-time type mismatch error unless explicit type conversion is applied.
var i int = 5
var f float64 = 2.0

// Invalid: mismatched types int and float64
// result := i / f 

// Valid: explicit conversion
result := float64(i) / f 
Exception: Untyped numeric constants are evaluated at compile-time and will implicitly assume the necessary type required by the expression context, provided the mathematical result does not overflow the target type.
Master Go with Deep Grasping Methodology!Learn More