Skip to main content
The - operator in Go functions as both a binary arithmetic operator for subtraction and a unary arithmetic operator for numeric negation. It operates exclusively on numeric types, including signed and unsigned integers, floating-point numbers, and complex numbers.

Binary Subtraction

As a binary operator, - computes the difference between two operands. Go enforces strict type safety; both operands must be of the exact same concrete type, or be untyped constants representable by the target type. Go does not perform implicit type coercion.
When operating on untyped numeric constants, the - operator yields an untyped constant. The default type of the result is determined by the operands (e.g., subtracting two untyped integers yields an untyped integer).

Unary Negation

As a unary operator, - yields the arithmetic inverse of its single operand.
Applying the unary - to an unsigned integer type is syntactically valid. It computes the two’s complement negation of the value, which effectively results in a wrap-around based on the bit-width of the unsigned type.

Overflow and Underflow Mechanics

Go handles arithmetic underflow and overflow silently at runtime without panicking.
  • Integers: Subtracting past the minimum value of a signed or unsigned integer type results in a silent two’s complement wrap-around.
  • Floating-point: Subtraction adheres to IEEE-754 standards. Subtracting values that exceed the precision or range limits will result in -Inf (negative infinity) or NaN (Not a Number), depending on the operands.
Tired of Poor Go Skills? Fix That With Deep Grasping!Learn More