go-delve/delve · error

invalid argument %s (type %s) to imag

Error message

invalid argument %s (type %s) to imag

What it means

Delve's `imag()` builtin only accepts complex64 or complex128 values. This error is returned when the (single) argument's reflect.Kind is not a complex kind, including the argument's source text and type in the message.

Source

Thrown at pkg/proc/eval.go:2029

	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 {
		return nil, fmt.Errorf("wrong number of arguments to real: %d", len(args))
	}

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

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

	if arg.Value == nil || ((arg.Value.Kind() != constant.Int) && (arg.Value.Kind() != constant.Float) && (arg.Value.Kind() != constant.Complex)) {

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Pass a complex-typed value: verify with `ptype <expr>` that its kind is complex64/complex128.
  2. For a float, the imaginary part is 0 — just use 0 instead of imag(f).
  3. If the value is an interface, evaluate the concrete field or assert its type first.
  4. Use `real(c)` if you actually wanted the real part.

Example fix

// before
imag(f)          // f is float64
// after
imag(c)          // c is complex128
real(f)          // or real for float input
Defensive patterns

Strategy: type-guard

Validate before calling

// before evaluating imag(x)
if v.Kind != reflect.Complex64 && v.Kind != reflect.Complex128 {
    return fmt.Errorf("imag requires complex64/complex128, got %s", v.TypeString())
}

Type guard

func isComplexKind(v *proc.Variable) bool {
    return v.Kind == reflect.Complex64 || v.Kind == reflect.Complex128
}

Try / catch

val, err := evalImag(expr)
if err != nil && strings.Contains(err.Error(), "invalid argument") {
    // fall back to 0 for floats, or report the type mismatch
}

Prevention

When it happens

Trigger: Evaluating `imag(x)` where x's Kind is Int, Float, String, etc. — i.e. not Complex64/Complex128 — after its value loaded successfully (eval.go:2029, imagBuiltin).

Common situations: Calling imag on a plain float (imagining floats have imaginary parts), or on an interface-typed value that Delve could not resolve to a complex concrete type.

Related errors


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