go-delve/delve · error

can not index %s with %s

Error message

can not index %s with %s

What it means

In evalArrayOrSliceExpression, the index expression must evaluate to an integer (or unsigned integer). When both idxev.asInt() and idxev.asUint() fail — the index is a string, float, bool, or otherwise non-numeric — the evaluator errors with 'can not index <x> with <index-expr>'. The check at this site also feeds breakpoint hit-count indexing.

Source

Thrown at pkg/proc/eval.go:2183

		stack.err = xev.Unreadable
		return
	}

	if xev.Name == evalop.BreakpointHitCountVarNameQualified {
		if idxev.Kind == reflect.String {
			s := constant.StringVal(idxev.Value)
			thc, err := totalHitCountByName(scope.target.Breakpoints().Logical, s)
			if err == nil {
				stack.push(newConstant(constant.MakeUint64(thc), scope.BinInfo, scope.Mem))
			}
			stack.err = err
			return
		}
		n, err := idxev.asInt()
		if err != nil {
			n2, err := idxev.asUint()
			if err != nil {
				stack.err = fmt.Errorf("can not index %s with %s", xev.Name, astutil.ExprToString(op.Node.Index))
				return
			}
			n = int64(n2)
		}
		thc, err := totalHitCountByID(scope.target.Breakpoints().Logical, int(n))
		if err == nil {
			stack.push(newConstant(constant.MakeUint64(thc), scope.BinInfo, scope.Mem))
		}
		stack.err = err
		return
	}

	if xev.Flags&VariableCPtr == 0 {
		xev = xev.maybeDereference()
	}

	cantindex := fmt.Errorf("expression %q (%s) does not support indexing", astutil.ExprToString(op.Node.X), xev.TypeString())

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Use an integer index expression: 'sl[2]' or 'sl[i]' where i is an int variable.
  2. If the operand is a map, ensure the map-index path is used (map[string]X supports 'm["key"]'); slices/arrays do not.
  3. Cast or convert the index: use an int-typed variable instead of a float or string.
  4. Check for typos where the index variable shadows a differently-typed symbol.

Example fix

// before
dlv> print sl["0"]
// error: can not index sl with "0"
// after
dlv> print sl[0]
Defensive patterns

Strategy: validation

Validate before calling

// Use integer literals or int-typed variables as indices:
// sl[0], sl[i]
// Never: sl["key"], sl[3.5], sl[flag]

Type guard

// Pre-validate the index expression kind before composing indexing:
if v := eval(idxExpr); v.Kind != reflect.Int && !isUintKind(v.Kind) {
    return fmt.Errorf("index %s must be an integer", idxExpr)
}

Try / catch

res, err := scope.EvalExpression(indexExpr, cfg)
if err != nil && strings.Contains(err.Error(), "can not index") {
    // fix the index to an integer expression
}

Prevention

When it happens

Trigger: Evaluating 'arr[<expr>]' where <expr> does not evaluate to an integer value, e.g. 'arr["key"]', 'arr[3.5]', 'sl[true]', or indexing with a struct/pointer-typed variable; also indexing breakpoint logical hit counts with a non-integer index.

Common situations: Copy-pasting map-style access ('m["k"]' syntax applied to slices/arrays), forgetting that map indexing is handled by a different path, or using a wrongly-typed loop variable in a condition.

Related errors


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