go-delve/delve · error
can not compare %s to nil
Error message
can not compare %s to nil
What it means
When comparing a value to nil, Delve only allows kinds that can be nil: pointer, unsafe pointer, chan, map, interface, slice and func. If the variable has any other kind (int, string, struct, etc.), the comparison `v == nil` is meaningless in Go and Delve rejects it with this error.
Source
Thrown at pkg/proc/eval.go:2487
} 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())
}
if xv.Unreadable != nil {
stack.err = xv.Unreadable
return
}
if yv.Kind != reflect.String { // delay loading strings until we use them
yv.loadValue(LoadFullValue())View on GitHub (pinned to a23773e6c3)
Solutions
- Verify the variable's kind with `print x` and drop the nil comparison for value types.
- If x is a struct, compare to a zero value instead: `p x == pkg.T{}`.
- If you need nil-ness of the underlying pointer, compare the pointer field: `p x.ptr == nil`.
Example fix
// before
p x == nil // x is a struct
// after
p x == pkg.T{} Defensive patterns
Strategy: type-guard
Validate before calling
// before: x == nil
switch v.Kind {
case reflect.Ptr, reflect.UnsafePointer, reflect.Chan, reflect.Map, reflect.Interface, reflect.Slice, reflect.Func:
// ok to compare to nil
default:
// compare to zero value instead
} Type guard
func canBeNil(k reflect.Kind) bool {
switch k {
case reflect.Ptr, reflect.UnsafePointer, reflect.Chan, reflect.Map, reflect.Interface, reflect.Slice, reflect.Func:
return true
}
return false
} Prevention
- Confirm the variable's kind with `print x` before comparing to nil.
- For structs, compare against the zero value `T{}`.
- Remember interfaces may surface their concrete kind in the debugger view.
When it happens
Trigger: Evaluating `p x == nil` or `p x != nil` in the debugger where x's reflect.Kind is a comparable non-nillable kind such as int, float64, string, bool or a plain struct.
Common situations: User assumes a variable is a pointer but it is actually a value type (e.g. an interface was resolved to its concrete struct, or a struct field was auto-dereferenced); a common source-state vs. debugger-view surprise.
Related errors
- operator %s can not be applied 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/dcbc748bd0f6532a.
Report an issue: GitHub.