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 == (equality) operator is a binary comparison operator that evaluates whether two operands are equal, yielding an untyped boolean value (true or false). For the operation to be valid, at least one operand must be assignable to the type of the other, and the types must be comparable according to Go’s type system. Go does not perform implicit type coercion during evaluation.
result := operand1 == operand2

Comparability Rules by Type

The behavior and validity of the == operator depend entirely on the operand types:
  • Basic Types (Booleans, Integers, Strings): Evaluated by value. Strings are equal if they have the same length and identical byte sequences.
  • Floating-Point and Complex Numbers: Evaluated by value following IEEE-754 standard semantics. Notably, NaN == NaN always evaluates to false, and -0.0 == 0.0 evaluates to true.
  • Pointers: Evaluated by memory address. Two pointers are equal if they point to the same underlying variable or if both are nil. Note: Pointers to distinct zero-size variables (e.g., &struct{}{}) may or may not evaluate as equal depending on compiler optimizations and escape analysis.
  • Channels: Evaluated by reference. Two channel variables are equal if they were instantiated by the same make invocation or if both are nil.
  • Structs: Evaluated by shallow field comparison. Two structs are equal if all corresponding fields (both exported and unexported) are equal. If a struct contains a pointer field, == compares the memory address of the pointer, not the dereferenced value. A struct is only comparable if all of its constituent field types are comparable.
  • Arrays: Evaluated by element-wise comparison. Two arrays are equal if they have the same length and all corresponding elements are equal. Arrays are only comparable if their element type is comparable.
// Struct and Array comparison syntax
type Node struct { 
    Value int
    Next  *Node 
}

// Shallow comparison: n1 and n2 are equal only if they point to the exact same Next address
n1 := Node{Value: 1, Next: &Node{}}
n2 := Node{Value: 1, Next: &Node{}}
isEqual := n1 == n2 // false (different pointer addresses for Next)

arr1, arr2 := [3]int{1, 2, 3}, [3]int{1, 2, 3}
isEqualArr := arr1 == arr2 // true

Interface Comparability and Runtime Panics

When applying == to interface values, Go compares both the dynamic type and the dynamic value. Two interface values are equal if:
  1. Both are nil.
  2. They possess identical dynamic types, and their underlying dynamic values are equal.
If the == operator is used on two interfaces that hold identical dynamic types, but that underlying type is strictly non-comparable (e.g., a slice), the compiler will allow the operation, but it will trigger a panic at runtime.
var i1 interface{} = []int{1, 2}
var i2 interface{} = []int{1, 2}

// Compiles successfully, but panics at runtime: 
// panic: runtime error: comparing uncomparable type []int
result := i1 == i2 

Non-Comparable Types

Slices, maps, and functions are strictly non-comparable. The Go compiler will reject any attempt to use the == operator to compare two variables of these types against each other. The only permitted use of == with slices, maps, or functions is to compare them directly against the predeclared identifier nil.
var s []int
// Valid: Comparing to nil
isNull := s == nil 

s2 := []int{}
// Invalid: Compiler error (invalid operation: s == s2 (slice can only be compared to nil))
// isMatch := s == s2 
Master Go with Deep Grasping Methodology!Learn More