go-delve/delve · error
operator %s can not be applied to "nil"
Error message
operator %s can not be applied to "nil"
What it means
In Go, nil comparison is only defined for == and !=; Delve enforces this in negotiateTypeNil, which handles comparisons of a variable against the nil literal. Any other operator (such as <, >, +) applied to nil yields this error before the kind check.
Source
Thrown at pkg/proc/eval.go:2481
return xv.DwarfType, nil
} else if xv.DwarfType != nil && yv.DwarfType == nil {
if err := yv.isType(xv.DwarfType, xv.Kind); err != nil {
return nil, err
}
return xv.DwarfType, nil
} else if xv.DwarfType == nil && yv.DwarfType != nil {
if err := xv.isType(yv.DwarfType, yv.Kind); err != nil {
return nil, err
}
return yv.DwarfType, nil
}
panic("unreachable")
}
func negotiateTypeNil(op token.Token, v *Variable) error {
if op != token.EQL && op != token.NEQ {
return fmt.Errorf("operator %s can not be applied to \"nil\"", op.String())
}
switch v.Kind {
case reflect.Ptr, reflect.UnsafePointer, reflect.Chan, reflect.Map, reflect.Interface, reflect.Slice, reflect.Func:
return nil
default:
return fmt.Errorf("can not compare %s to nil", v.Kind.String())
}
}
func (scope *EvalScope) evalBinary(binop *evalop.Binary, stack *evalStack) {
node := binop.Node
yv := stack.pop()
xv := stack.pop()
if xv.Kind != reflect.String { // delay loading strings until we use them
xv.loadValue(LoadFullValue())
}View on GitHub (pinned to a23773e6c3)
Solutions
- Use == or != when comparing against nil: `p ptr == nil`.
- Dereference before applying other operators, guarding the nil case first.
- If testing emptiness of a map/slice, compare with len(): `p len(s) == 0`.
Example fix
// before p ptr < nil // after p ptr == nil
Defensive patterns
Strategy: validation
Validate before calling
// only == / != are valid against nil
if op != token.EQL && op != token.NEQ {
// rewrite the expression using == or !=
} Type guard
func nilComparable(op token.Token) bool {
return op == token.EQL || op == token.NEQ
} Prevention
- Restrict nil usage to equality checks.
- Guard arithmetic with nil checks before dereferencing pointers.
- Use len() == 0 for slice/map emptiness instead of nil comparisons.
When it happens
Trigger: Evaluating an expression like `p nil < x`, `p nil + 1`, or any binary op other than EQL/NEQ where one operand is the nil keyword; negotiateTypeNil is called when one operand is nilVariable.
Common situations: Typo in an interactive expression during debugging, e.g. intending `ptr == nil` but typing `ptr < nil`, or attempting arithmetic involving nil while probing pointer logic.
Related errors
- can not compare %s to nil
- shift count must not be negative
- non-zero length array with nil base
- invalid argument %s (type %s) for cap
- wrong number of arguments to len: %d
AI-assisted analysis of go-delve/delve@a23773e6c3 (2026-08-31).
Data as JSON: /api/errors/af4e242a756e846b.
Report an issue: GitHub.