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 != (inequality) operator is a binary comparison operator that evaluates whether two operands are not equal. It returns an untyped boolean value: true if the operands differ in value, and false if they are identical.
var operand1, operand2 int
_ = operand1 != operand2
Because Go is statically and strictly typed, the != operator does not perform implicit type coercion. For the expression to compile, the operands must satisfy two strict language mechanics: assignability and comparability.

Assignability

operand1 must be assignable to the type of operand2, or vice versa. You cannot directly compare an int to a float64 without an explicit type conversion.
var a int = 5
var b float64 = 5.0

// Invalid: mismatched types int and float64
// _ = a != b 

// Valid: explicit type conversion
_ = float64(a) != b 

Comparability Rules

The Go language specification defines exactly which types are comparable. The != operator behaves differently depending on the underlying types being evaluated:
  • Basic Types (Booleans, Numerics, Strings): Evaluates to true if the underlying scalar values differ. Floating-point inequality adheres to IEEE-754 standards (e.g., NaN != NaN is always true).
  • Pointers: Evaluates to true if the pointers hold different memory addresses, or if one is nil and the other is not.
  • Channels: Evaluates to true if the channel references point to different underlying data structures (created by different make calls), or if one is nil and the other is not.
  • Interfaces: Evaluates to true if the two interface values have different dynamic types, or if they have the same comparable dynamic type and their underlying dynamic values are not equal. Critical behavior: If the interfaces share the same dynamic type but that dynamic type is non-comparable (e.g., a slice, map, or function), the != operation will trigger a run-time panic.
  • Structs: Evaluates to true if any corresponding non-blank fields between the two structs are not equal. Blank fields (_) are explicitly ignored during the evaluation of equality. However, a struct is only comparable at compile-time if all of its fields—including blank fields—are of comparable types. If a struct contains a blank field of a non-comparable type, the entire struct becomes non-comparable. If a struct contains interface fields, the comparison cascades and is subject to the same run-time panic risk if the interface holds a non-comparable dynamic type.
  • Arrays: Evaluates to true if any corresponding elements at the same index differ. Arrays are only comparable if their element type is comparable. Like structs, arrays of interfaces can trigger a run-time panic during comparison.
// Struct comparison mechanics
type Point struct { 
    X, Y int 
    _    int // Blank fields are ignored during evaluation, but MUST be of a comparable type
}
p1 := Point{X: 1, Y: 2}
p2 := Point{X: 1, Y: 3}
_ = p1 != p2 // true, because the Y fields differ

type InvalidPoint struct {
    X int
    _ []int // Non-comparable type makes the entire struct non-comparable
}
// var ip1, ip2 InvalidPoint
// _ = ip1 != ip2 // Compile-time error: InvalidPoint is not comparable

// Interface run-time panic mechanics
var i1 interface{} = []int{1, 2}
var i2 interface{} = []int{1, 2}
// Compiles successfully, but crashes at runtime:
// panic: runtime error: comparing uncomparable type []int
// _ = i1 != i2 

Non-Comparable Types

Slices, maps, and functions are explicitly non-comparable in Go. You cannot use the != operator to compare two slices, two maps, or two functions against each other directly. Attempting to do so results in a compile-time error. The only valid use of the != operator with slices, maps, or functions is to compare them against the predeclared identifier nil.
slice1 := []int{1, 2, 3}

// ERROR: invalid operation: slice1 != slice2 (slice can only be compared to nil)
// slice2 := []int{1, 2, 3}
// _ = slice1 != slice2 

// Valid: checking against nil
_ = slice1 != nil // true
Master Go with Deep Grasping Methodology!Learn More