go-delve/delve · error

can not index %q

Error message

can not index %q

What it means

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.

Source

Thrown at pkg/proc/eval.go:2220

	switch xev.Kind {
	case reflect.Ptr:
		if xev == nilVariable {
			stack.err = cantindex
			return
		}
		if xev.Flags&VariableCPtr == 0 {
			_, isarrptr := xev.RealType.(*godwarf.PtrType).Type.(*godwarf.ArrayType)
			if !isarrptr {
				stack.err = cantindex
				return
			}
			xev = xev.maybeDereference()
		}
		fallthrough

	case reflect.Slice, reflect.Array, reflect.String:
		if xev.Base == 0 {
			stack.err = fmt.Errorf("can not index %q", astutil.ExprToString(op.Node.X))
			return
		}
		n, err := idxev.asInt()
		if err != nil {
			stack.err = err
			return
		}
		stack.pushErr(xev.sliceAccess(int(n)))
		return

	case reflect.Map:
		idxev.loadValue(LoadFullValue())
		if idxev.Unreadable != nil {
			stack.err = idxev.Unreadable
			return
		}
		stack.pushErr(xev.mapAccess(idxev))
		return

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Verify the slice/string is initialized before the index point: print it first ('print s').
  2. Move the evaluation/condition after the make/append that allocates the backing array.
  3. Guard conditions with 'len(s) > 0 && s[0] ...'.
  4. If debugging a core dump, recapture with the memory pages containing the backing array included.

Example fix

// before (s is nil slice)
cond 1 s[0] == 42
// error: can not index "s"
// after
cond 1 len(s) > 0 && s[0] == 42
Defensive patterns

Strategy: validation

Validate before calling

// Guard with length before indexing in conditions:
// len(s) > 0 && s[0] ...
// CLI precheck: dlv> print s   // nil slices print as []T(nil)

Try / catch

res, err := scope.EvalExpression(indexExpr, cfg)
if err != nil && strings.Contains(err.Error(), "can not index") {
    // the operand's backing array is nil/unallocated; initialize before use
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of go-delve/delve@a23773e6c3 (2026-08-31). Data as JSON: /api/errors/ef88905b80265b7c. Report an issue: GitHub.