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 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.
var a int32 = 42
var b int32 = 10
diff := a - b // Valid: both are int32

var c float64 = 5.5
// invalid := a - c // Compilation error: mismatched types int32 and float64
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).
const x = 10 - 3.0 // Untyped floating-point constant, value 7.0

Unary Negation

As a unary operator, - yields the arithmetic inverse of its single operand.
var x int = 15
negX := -x // Value: -15

var y complex64 = 3 + 4i
negY := -y // Value: -3 - 4i
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.
var u uint8 = 1
negU := -u // Type: uint8, Value: 255 (binary 11111111)

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.
// Integer underflow
var minVal uint8 = 0
wrap := minVal - 1 // Value: 255

// Floating-point infinity
var f1 float64 = -1e308
var f2 float64 = 1e308
infResult := f1 - f2 // Value: -Inf
Master Go with Deep Grasping Methodology!Learn More