{"record":{"id":"dcbc748bd0f6532a","repo":"go-delve/delve","slug":"can-not-compare-s-to-nil","errorCode":null,"errorMessage":"can not compare %s to nil","messagePattern":"can not compare (.+?) to nil","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/proc/eval.go","lineNumber":2487,"sourceCode":"\t} else if xv.DwarfType == nil && yv.DwarfType != nil {\n\t\tif err := xv.isType(yv.DwarfType, yv.Kind); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\treturn yv.DwarfType, nil\n\t}\n\n\tpanic(\"unreachable\")\n}\n\nfunc negotiateTypeNil(op token.Token, v *Variable) error {\n\tif op != token.EQL && op != token.NEQ {\n\t\treturn fmt.Errorf(\"operator %s can not be applied to \\\"nil\\\"\", op.String())\n\t}\n\tswitch v.Kind {\n\tcase reflect.Ptr, reflect.UnsafePointer, reflect.Chan, reflect.Map, reflect.Interface, reflect.Slice, reflect.Func:\n\t\treturn nil\n\tdefault:\n\t\treturn fmt.Errorf(\"can not compare %s to nil\", v.Kind.String())\n\t}\n}\n\nfunc (scope *EvalScope) evalBinary(binop *evalop.Binary, stack *evalStack) {\n\tnode := binop.Node\n\n\tyv := stack.pop()\n\txv := stack.pop()\n\n\tif xv.Kind != reflect.String { // delay loading strings until we use them\n\t\txv.loadValue(LoadFullValue())\n\t}\n\tif xv.Unreadable != nil {\n\t\tstack.err = xv.Unreadable\n\t\treturn\n\t}\n\tif yv.Kind != reflect.String { // delay loading strings until we use them\n\t\tyv.loadValue(LoadFullValue())","sourceCodeStart":2469,"sourceCodeEnd":2505,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/proc/eval.go#L2469-L2505","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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`."],"exampleFix":"// before\np x == nil   // x is a struct\n// after\np x == pkg.T{}","handlingStrategy":"type-guard","validationCode":"// before: x == nil\nswitch v.Kind {\ncase reflect.Ptr, reflect.UnsafePointer, reflect.Chan, reflect.Map, reflect.Interface, reflect.Slice, reflect.Func:\n    // ok to compare to nil\ndefault:\n    // compare to zero value instead\n}","typeGuard":"func canBeNil(k reflect.Kind) bool {\n    switch k {\n    case reflect.Ptr, reflect.UnsafePointer, reflect.Chan, reflect.Map, reflect.Interface, reflect.Slice, reflect.Func:\n        return true\n    }\n    return false\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["debugger","expression-evaluation","nil"],"backgroundTag":"compare-value-to-nil","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}