go-delve/delve · error

can not slice %q (type %s)

Error message

can not slice %q (type %s)

What it means

Thrown by evalop.Slice handling when the operand of a slice expression is not a sliceable kind (Slice, Array, String) and is not a C pointer (VariableCPtr). The evaluator falls through to the default case and reports the operand's Go type. It means slice syntax was applied to something intrinsically unsliceable, like an int, struct, map, or function.

Source

Thrown at pkg/proc/eval.go:2298

			stack.err = errors.New("second slice argument must be empty for maps")
			return
		}
		xev.mapSkip += int(low)
		xev.mapIterator(0) // reads map length
		if int64(xev.mapSkip) >= xev.Len {
			stack.err = errors.New("map index out of bounds")
			return
		}
		stack.push(xev)
		return
	case reflect.Ptr:
		if xev.Flags&VariableCPtr != 0 {
			stack.pushErr(xev.reslice(low, high, op.TrustLen))
			return
		}
		fallthrough
	default:
		stack.err = fmt.Errorf("can not slice %q (type %s)", astutil.ExprToString(op.Node.X), xev.TypeString())
		return
	}
}

// Evaluates a pointer dereference expression: *<subexpr>
func (scope *EvalScope) evalPointerDeref(op *evalop.PointerDeref, stack *evalStack) {
	xev := stack.pop()

	if xev.Kind != reflect.Ptr {
		stack.err = fmt.Errorf("expression %q (%s) can not be dereferenced", astutil.ExprToString(op.Node.X), xev.TypeString())
		return
	}

	if xev == nilVariable {
		stack.err = errors.New("nil can not be dereferenced")
		return
	}

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Print the operand (`print x`) and check its type in the error message; use the operation matching that type (indexing `[k]` for maps/arrays, slicing only for slice/array/string).
  2. If you intended string/slice slicing, verify you are referencing the right variable name in this scope.
  3. For Go pointers, dereference first (`*p`) then slice the result; raw pointer slicing is only supported for C pointers.
  4. If the type is unexpected, check for shadowing or a type change (e.g. variable reassigned to a different type) in the current function.

Example fix

// before (x is an int at dlv prompt)
(dlv) print x[0:4]
can not slice "x[0:4]" (type int)

// after: convert or index the correct object
(dlv) print string(rune(x)) // or slice the actual string variable
(dlv) print myString[0:4]
Defensive patterns

Strategy: validation

Validate before calling

// Check operand type before applying slice syntax:
(dlv) print x  // read the reported type from the output
// Only use x[a:b] when the type is slice, array, or string.

Type guard

func isSliceable(v interface{}) bool {
    switch reflect.TypeOf(v).Kind() {
    case reflect.Slice, reflect.Array, reflect.String:
        return true
    }
    return false
}

Prevention

When it happens

Trigger: Evaluating `x[0:1]` where x has kind Int, Float, Bool, Struct, Map, Chan, Func, etc., or slicing a map with a [low:high] form (maps only support [k] indexing, not ranges). Also triggered by plain pointer arithmetic attempts unless the variable is flagged as a C pointer.

Common situations: Typo where the user meant to index instead of slice (`m[a:b]` on a map), slicing a numeric variable thinking it's a string, attempting C-style pointer slicing on a regular Go pointer (only C pointers created via cast get VariableCPtr), or confusion after a variable changed type.

Related errors


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