go-delve/delve · error

wrong number of arguments to imag: %d

Error message

wrong number of arguments to imag: %d

What it means

The `imag()` builtin in Delve's evaluator extracts the imaginary part of a complex value and requires exactly one argument. Any other count returns this arity error before type checking.

Source

Thrown at pkg/proc/eval.go:2018

		if isz > sz {
			sz = isz
		}
	}

	if sz == 0 {
		sz = 128
	}

	typ := godwarf.FakeBasicType("complex", int(sz))

	r := realev.newVariable("", 0, typ, nil)
	r.Value = constant.BinaryOp(realev.Value, token.ADD, constant.MakeImag(imagev.Value))
	return r, nil
}

func imagBuiltin(args []*Variable, nodeargs []ast.Expr) (*Variable, error) {
	if len(args) != 1 {
		return nil, fmt.Errorf("wrong number of arguments to imag: %d", len(args))
	}

	arg := args[0]
	arg.loadValue(loadSingleValue)

	if arg.Unreadable != nil {
		return nil, arg.Unreadable
	}

	if arg.Kind != reflect.Complex64 && arg.Kind != reflect.Complex128 {
		return nil, fmt.Errorf("invalid argument %s (type %s) to imag", astutil.ExprToString(nodeargs[0]), arg.TypeString())
	}

	return newConstant(constant.Imag(arg.Value), arg.bi, arg.mem), nil
}

func realBuiltin(args []*Variable, nodeargs []ast.Expr) (*Variable, error) {
	if len(args) != 1 {

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Pass exactly one complex-typed argument: `imag(c)`.
  2. To build a complex value use `complex(re, im)` instead.
  3. Validate argument count programmatically before calling the evaluator.

Example fix

// before
imag(re, im)
// after
complex(re, im)
imag(c)
Defensive patterns

Strategy: validation

Validate before calling

args, err := parseCallArgs(expr)
if err != nil || len(args) != 1 {
    return fmt.Errorf("imag takes exactly 1 argument, got %d", len(args))
}

Try / catch

val, err := eval(expr)
if err != nil && strings.Contains(err.Error(), "wrong number of arguments to imag") {
    // fix the call arity and retry
}

Prevention

When it happens

Trigger: Evaluating `imag()` with zero arguments or `imag(c, extra)` with two or more in the debugger console or eval API (eval.go:2018, imagBuiltin).

Common situations: Typos like `imag(re, im)` (confusing imag with complex), or programmatic expression builders supplying the wrong number of args.

Related errors


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