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 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.
var a int32 = 15
var b int32 = 5
var difference int32 = a - b // Evaluates to 10
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.
var x int = 10
var y float64 = 3.5
// result := x - y // Compilation error: invalid operation: mismatched types int and float64
result := float64(x) - y

Unary Negation

As a unary operator, - precedes a single operand and yields its arithmetic negation.
var val float64 = 4.2
var neg float64 = -val // Evaluates to -4.2

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.
var minInt8 int8 = -128
var underflow int8 = minInt8 - 1 // Evaluates to 127 (wraps to max int8)
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.
var u uint8 = 0
var sub uint8 = u - 1 // Evaluates to 255

var v uint8 = 5
var negV uint8 = -v   // Evaluates to 251 (256 - 5)
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).
var f1 float64 = math.Inf(1)
var f2 float64 = math.Inf(1)
var f3 float64 = f1 - f2 // Evaluates to 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.
const huge = 1 << 100
const diff = huge - (huge - 1) // Evaluates at compile-time to untyped constant 1
Master Go with Deep Grasping Methodology!Learn More