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 applies exclusively to numeric types (integer, floating-point, and complex numbers) and untyped numeric constants.

Binary Subtraction

As a binary operator, - computes the difference between two operands.
Type Strictness: Go requires both operands to be of the exact same type. The compiler does not perform implicit type coercion. If the operands are of different types, an explicit type conversion is required.

Unary Negation

As a unary operator, - precedes a single operand and yields its arithmetic negation.

Arithmetic Mechanics and Edge Cases

Integer Overflow and Underflow: Go does not panic on integer underflow or overflow. Arithmetic operations on integer types wrap around silently based on two’s complement representation.
Unsigned Integer Negation and Subtraction: Subtracting a larger unsigned integer from a smaller one wraps around to the upper bound of the type’s capacity. Applying the unary - operator to an unsigned integer x is mathematically defined as 2^n - x (where n is the bit width of the type), effectively yielding the two’s complement wrap-around value.
Floating-Point and Complex Numbers: For float32, float64, complex64, and complex128, the - operator strictly adheres to the IEEE-754 standard. This includes the correct handling of signed zeros (-0.0), Infinity (+Inf, -Inf), and Not-a-Number (NaN).
Untyped Constants: When the - operator is applied to untyped numeric constants, the operation is evaluated at compile-time with arbitrary precision, bypassing the size limitations and overflow characteristics of standard typed integers until the constant is assigned to a typed variable.
Tired of Poor Go Skills? Fix That With Deep Grasping!Learn More