{"record":{"id":"ef88905b80265b7c","repo":"go-delve/delve","slug":"can-not-index-q","errorCode":null,"errorMessage":"can not index %q","messagePattern":"can not index %q","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/proc/eval.go","lineNumber":2220,"sourceCode":"\tswitch xev.Kind {\n\tcase reflect.Ptr:\n\t\tif xev == nilVariable {\n\t\t\tstack.err = cantindex\n\t\t\treturn\n\t\t}\n\t\tif xev.Flags&VariableCPtr == 0 {\n\t\t\t_, isarrptr := xev.RealType.(*godwarf.PtrType).Type.(*godwarf.ArrayType)\n\t\t\tif !isarrptr {\n\t\t\t\tstack.err = cantindex\n\t\t\t\treturn\n\t\t\t}\n\t\t\txev = xev.maybeDereference()\n\t\t}\n\t\tfallthrough\n\n\tcase reflect.Slice, reflect.Array, reflect.String:\n\t\tif xev.Base == 0 {\n\t\t\tstack.err = fmt.Errorf(\"can not index %q\", astutil.ExprToString(op.Node.X))\n\t\t\treturn\n\t\t}\n\t\tn, err := idxev.asInt()\n\t\tif err != nil {\n\t\t\tstack.err = err\n\t\t\treturn\n\t\t}\n\t\tstack.pushErr(xev.sliceAccess(int(n)))\n\t\treturn\n\n\tcase reflect.Map:\n\t\tidxev.loadValue(LoadFullValue())\n\t\tif idxev.Unreadable != nil {\n\t\t\tstack.err = idxev.Unreadable\n\t\t\treturn\n\t\t}\n\t\tstack.pushErr(xev.mapAccess(idxev))\n\t\treturn","sourceCodeStart":2202,"sourceCodeEnd":2238,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/proc/eval.go#L2202-L2238","documentation":"For slice/array/string indexing, if the operand's Base address is 0 — meaning the underlying data pointer is nil or points nowhere — the evaluator refuses to index with 'can not index %q'. This catches indexing nil slices, nil maps falling here, or strings/arrays whose backing memory address is zero.","triggerScenarios":"Evaluating 'x[i]' where x is a nil slice ('var s []int; s[0]'), a nil string pointer-backed value, or an array/slice variable whose Base was never set (uninitialized backing array) at evaluation time.","commonSituations":"Inspecting slices that were declared but never allocated/made, breakpoint conditions evaluated before the slice is initialized, or corrupted/unloaded memory in core dumps where the backing array address is 0.","solutions":["Verify the slice/string is initialized before the index point: print it first ('print s').","Move the evaluation/condition after the make/append that allocates the backing array.","Guard conditions with 'len(s) > 0 && s[0] ...'.","If debugging a core dump, recapture with the memory pages containing the backing array included."],"exampleFix":"// before (s is nil slice)\ncond 1 s[0] == 42\n// error: can not index \"s\"\n// after\ncond 1 len(s) > 0 && s[0] == 42","handlingStrategy":"validation","validationCode":"// Guard with length before indexing in conditions:\n// len(s) > 0 && s[0] ...\n// CLI precheck: dlv> print s   // nil slices print as []T(nil)","typeGuard":null,"tryCatchPattern":"res, err := scope.EvalExpression(indexExpr, cfg)\nif err != nil && strings.Contains(err.Error(), \"can not index\") {\n    // the operand's backing array is nil/unallocated; initialize before use\n}","preventionTips":["ensure slices are allocated (make/append) before evaluation points","guard conditions with len(s) > 0 before s[0]","avoid evaluating bp conditions before slice initialization runs","with core dumps, confirm backing-array pages are captured"],"tags":["debugger","go","indexing","nil-slice"],"backgroundTag":"index-nil-slice","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}