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 <= (less than or equal to) operator is a binary relational operator that evaluates whether the left operand is strictly less than or mathematically equal to the right operand. It yields an untyped boolean value (true or false).
result := operand1 <= operand2

Type Constraints and Assignability

In Go, the <= operator requires the operands to be of the same type, or one operand must be implicitly assignable to the type of the other. When comparing a typed variable with an untyped constant, the constant must be representable by a value of the variable’s type. For example, comparing an int variable to the untyped constant 5.0 is valid because 5.0 is representable as an integer, but comparing it to 5.5 will result in a compile-time error. Furthermore, the operands must belong to an ordered type. Ordered types in Go consist of:
  • Integer types: int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, byte, and rune.
  • Floating-point types: float32 and float64.
  • String types: string.
  • User-defined types: Any custom type whose underlying type is one of the ordered types listed above (e.g., type CustomInt int).
Attempting to use <= with unordered types—such as booleans, complex numbers (complex64, complex128), pointers, channels, interfaces, or composite types (structs, arrays, maps, slices)—will result in a compile-time error.

Evaluation Mechanics

Numeric Evaluation For integers and floating-point numbers, the operator evaluates standard mathematical ordering. Floating-point comparisons adhere to IEEE-754 specifications:
  • -0.0 <= +0.0 evaluates to true.
  • If either operand is NaN (Not a Number), the result is always false.
String Evaluation Strings are evaluated lexicographically, byte-by-byte. The operator compares the raw byte values of the UTF-8 encoded strings. If the bytes at a given index are identical, it proceeds to the next byte. A shorter string is considered less than a longer string if all bytes up to the length of the shorter string are equal.

Syntax Visualization

// Integer evaluation
var x, y int = 10, 10
isLessOrEq := x <= y // true

// Floating-point evaluation with IEEE-754 rules
var f1, f2 float64 = 3.14, 5.0
floatRes := f1 <= f2 // true

// String evaluation (lexicographical byte comparison)
s1, s2 := "apple", "banana"
strRes := s1 <= s2 // true (byte value of 'a' is less than 'b')

// User-defined ordered type evaluation
type CustomInt int
var c1, c2 CustomInt = 4, 7
customRes := c1 <= c2 // true

// Untyped constant representability
var a int = 5
validConstRes := a <= 5.0 // true (5.0 is representable as an int)
// invalidConstRes := a <= 5.5 // compile-time error: constant 5.5 truncated to integer

// Invalid: Type mismatch (Compile-time error)
// var b float64 = 5.0
// errRes := a <= b // compile-time error: mismatched types int and float64
Master Go with Deep Grasping Methodology!Learn More