go-delve/delve · error

invalid argument (type ) for len

Error message

invalid argument %s (type %s) for len

What it means

Delve's `len()` builtin throws this when its single argument's type has no length: valid kinds are string, slice, map, channel, array (or pointer to array after dereference). The error message includes the argument's source text and type string.

Solutions

  1. Verify the argument type with `ptype <expr>`; len needs string/slice/map/chan/array.
  2. For numbers or bools, drop len entirely.
  3. For a struct, len the specific slice/map/string field: `len(s.field)`.
  4. If a pointer should point at an array, dereference and confirm the pointee type.

Example fix

// before
len(myStruct)
// after
len(myStruct.Items)
Defensive patterns

Strategy: type-guard

Validate before calling

// before evaluating len(x)
switch v.Kind {
case reflect.String, reflect.Slice, reflect.Map, reflect.Chan, reflect.Array:
    // ok
case reflect.Ptr:
    // ok only if dereferenced kind is Array
default:
    return fmt.Errorf("len not defined on %s", v.TypeString())
}

Type guard

func isLenable(v *proc.Variable) bool {
    switch v.Kind {
    case reflect.String, reflect.Slice, reflect.Map, reflect.Chan, reflect.Array:
        return true
    case reflect.Ptr:
        return v.MaybeDereference().Kind == reflect.Array
    }
    return false
}

Try / catch

val, err := evalLen(expr)
if err != nil && strings.Contains(err.Error(), "invalid argument") {
    // print the type and suggest the correct field/builtin
}

Prevention

When it happens

Trigger: Evaluating `len(x)` where x's reflect.Kind is Int, Float, Struct, Func, Bool, Ptr-to-non-array, etc. (eval.go:1930, lenBuiltin).

Common situations: `len(myInt)`, `len(myStruct)`, `len(funcVal)`, or a pointer arg that dereferences to a non-array, plus cases where optimized-out variables resolve to an unexpected type.

Related errors


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

Appendix: source

Thrown at pkg/proc/eval.go:1930

		arg.loadValue(LoadFullValue())
		if arg.Unreadable != nil {
			return nil, arg.Unreadable
		}
		if arg.Base == 0 {
			return newConstant(constant.MakeInt64(0), arg.bi, arg.mem), nil
		}
		return newConstant(arg.Children[1].Value, arg.bi, arg.mem), nil
	default:
		return nil, invalidArgErr
	}
}

func lenBuiltin(args []*Variable, nodeargs []ast.Expr) (*Variable, error) {
	if len(args) != 1 {
		return nil, fmt.Errorf("wrong number of arguments to len: %d", len(args))
	}
	arg := args[0]
	invalidArgErr := fmt.Errorf("invalid argument %s (type %s) for len", astutil.ExprToString(nodeargs[0]), arg.TypeString())

	switch arg.Kind {
	case reflect.Ptr:
		arg = arg.maybeDereference()
		if arg.Kind != reflect.Array {
			return nil, invalidArgErr
		}
		fallthrough
	case reflect.Array, reflect.Slice, reflect.String:
		if arg.Unreadable != nil {
			return nil, arg.Unreadable
		}
		return newConstant(constant.MakeInt64(arg.Len), arg.bi, arg.mem), nil
	case reflect.Chan:
		arg.loadValue(LoadFullValue())
		if arg.Unreadable != nil {
			return nil, arg.Unreadable
		}

View on GitHub (pinned to a23773e6c3)