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

Type Constraints

In Go, the < operator strictly requires both operands to be of the same ordered type. Go does not perform implicit type coercion between distinct concrete types. If the operands are of different types, the compiler will throw a mismatched types error unless one or both are untyped constants that can be implicitly converted to a common type.

Supported Ordered Types and Evaluation Mechanics

  • Integer Types (int, uint, int8, byte, etc.): Evaluates the standard mathematical value. Signed and unsigned integers are evaluated according to their respective bitwise representations.
  • Floating-Point Types (float32, float64): Evaluates numerical value according to the IEEE-754 standard.
    • Positive zero (+0.0) and negative zero (-0.0) are considered equal, so -0.0 < +0.0 is false.
    • If either operand is NaN (Not a Number), the result is always false.
  • String Types: Evaluates lexicographically byte-by-byte, not by rune or locale. The operator compares the raw byte values at each index. If all bytes match up to the length of the shorter string, the shorter string is evaluated as less than the longer string.

Unsupported Types

The < operator is not defined for unordered types. Attempting to use < with the following types results in a compile-time error:
  • Booleans
  • Complex numbers (complex64, complex128)
  • Pointers
  • Channels, Interfaces, Maps, Slices, Arrays, and Structs

Syntax Visualization

package main

import "math"

func evaluateLessThan() {
	// Integer evaluation
	var a int = 5
	var b int = 10
	res1 := a < b // true

	// Floating-point evaluation with NaN
	res2 := math.NaN() < 5.0 // false

	// String evaluation (lexicographical by byte value)
	var s1 string = "apple"
	var s2 string = "banana"
	res3 := s1 < s2 // true ('a' byte value 97 < 'b' byte value 98)

	// String evaluation (length fallback)
	var s3 string = "cat"
	var s4 string = "cats"
	res4 := s3 < s4 // true (bytes match, s3 is shorter)

	// Compile-time Error: Mismatched types
	// var x int32 = 5
	// var y int64 = 10
	// _ = x < y // invalid operation: x < y (mismatched types int32 and int64)

	// Compile-time Error: Unordered type
	// var c1 complex64 = 1 + 2i
	// var c2 complex64 = 2 + 3i
	// _ = c1 < c2 // invalid operation: c1 < c2 (operator < not defined on complex64)

	// Blank identifiers to prevent "declared and not used" compiler errors
	_, _, _, _ = res1, res2, res3, res4
}
Master Go with Deep Grasping Methodology!Learn More