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 ! (logical NOT) operator is a unary operator that negates a boolean expression. It evaluates to true if its operand is false, and evaluates to false if its operand is true.
!operand

Type Constraints

Go enforces strict type safety for logical operations. The operand supplied to the ! operator must resolve to a typed or untyped bool. Unlike languages such as C or JavaScript, Go does not implement “truthy” or “falsy” coercion. You cannot apply the ! operator to integers, pointers, strings, or nil values. Attempting to negate a non-boolean type results in a compile-time type mismatch error.
var isSet bool = true
var isNotSet bool = !isSet // Evaluates to false

// Untyped boolean constant evaluation
const inverted = !false    // Evaluates to true

// INVALID: Go does not coerce non-boolean types
// var count int = 1
// var noCount bool = !count // Compile error: invalid operation: !count (operator ! not defined on int)

Operator Precedence

The ! operator possesses the highest precedence among logical operators. In Go, unary operators (including !, unary *, unary &, and unary -) have the highest overall precedence, sitting strictly above the five precedence levels defined for binary operators. Because of this highest precedence, ! binds immediately to the expression directly to its right before any binary logical (&&, ||) or relational (==, !=) operators are evaluated.
var x, y bool = false, true

// The ! operator binds strictly to 'x'.
// Evaluation order: (!x) && y -> (true) && true -> true
result := !x && y 

// To negate the result of a binary expression, parentheses are required.
// Evaluation order: !(x && y) -> !(false) -> true
groupedResult := !(x && y)
Master Go with Deep Grasping Methodology!Learn More