go-delve/delve · error

can not convert value of type %s to int

Error message

can not convert value of type %s to int

What it means

This error comes from Variable.asInt() in Delve's expression evaluator. When a debugger expression is converted to an integer, the variable's DWARF type must be an *godwarf.IntType (a signed integer). If the value is any other DWARF type (unsigned int, float, string, pointer, struct, etc.), Delve refuses the conversion and returns this error instead of producing a wrong number.

Source

Thrown at pkg/proc/eval.go:2731

		if !r && shortcircuit {
			return false, nil
		}
	}
	return r, nil
}

func (v *Variable) asInt() (int64, error) {
	if v.DwarfType == nil {
		if v.Value.Kind() != constant.Int {
			return 0, fmt.Errorf("can not convert constant %s to int", v.Value)
		}
	} else {
		v.loadValue(loadSingleValue)
		if v.Unreadable != nil {
			return 0, v.Unreadable
		}
		if _, ok := v.DwarfType.(*godwarf.IntType); !ok {
			return 0, fmt.Errorf("can not convert value of type %s to int", v.DwarfType.String())
		}
	}
	n, _ := constant.Int64Val(v.Value)
	return n, nil
}

func (v *Variable) asUint() (uint64, error) {
	if v.DwarfType == nil {
		if v.Value.Kind() != constant.Int {
			return 0, fmt.Errorf("can not convert constant %s to uint", v.Value)
		}
	} else {
		v.loadValue(loadSingleValue)
		if v.Unreadable != nil {
			return 0, v.Unreadable
		}
		if _, ok := v.DwarfType.(*godwarf.UintType); !ok {
			return 0, fmt.Errorf("can not convert value of type %s to uint", v.DwarfType.String())

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Use the correct cast: convert to uint64(...) or float64(...) matching the variable's actual DWARF type, or cast the source variable to a signed integer type in your program first
  2. Check the variable's type with the 'whatis' command before applying an int cast
  3. If the value is constant (v.DwarfType == nil path is fine), ensure the constant literal itself is integral; rewrite the literal without a fractional or string form
  4. If this happens on a variable you believe is signed int, verify DWARF debug info is complete (compile with the same Go version, no stripped symbols)

Example fix

// before (debugger expression)
print(int(uintVal))
// after
print(int64(uintVal))   // or print(uintVal) directly
Defensive patterns

Strategy: validation

Validate before calling

// Delve debugger expression pre-check
// whatis myVar  -> confirm DwarfType is a signed int before casting
print(int64(myVar)) // use int64/float64 casts matching the actual type

Type guard

// conceptual: only cast when type is a signed integer
// if strings.HasPrefix(whatisOutput, "int") { cast } else { use native type }

Prevention

When it happens

Trigger: Calling APIs that coerce a Variable to an integer: Variable.asInt() invoked by evaluating expressions like int(x), or internal uses such as array index computation and constant conversion in eval.go, when v.DwarfType is non-nil but not *godwarf.IntType.

Common situations: Evaluating int(myUintVar) or int(myFloat) in the debugger console; casting a pointer or string to int in a breakpoint condition; scripts/plugins calling eval APIs expecting a numeric result from an unsigned-typed variable.

Related errors


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