go-delve/delve · error
mismatched types nil and %s
Error message
mismatched types nil and %s
What it means
Raised by the type-conversion logic (convertToType) when the source expression is untyped nil (nilVariable). nil is only assignable to slice, map, func, pointer, chan, and interface types in Go. If the target type typ is anything else (int, string, struct, bool...), Delve reports 'mismatched types nil and <target>'.
Source
Thrown at pkg/proc/eval.go:2781
func (v *Variable) isType(typ godwarf.Type, kind reflect.Kind) error {
if v.DwarfType != nil {
if typ == nil || !sameType(typ, v.RealType) {
return &typeConvErr{v.DwarfType, typ}
}
return nil
}
if typ == nil {
return nil
}
if v == nilVariable {
switch kind {
case reflect.Slice, reflect.Map, reflect.Func, reflect.Ptr, reflect.Chan, reflect.Interface:
return nil
default:
return fmt.Errorf("mismatched types nil and %s", typ.String())
}
}
converr := fmt.Errorf("can not convert %s constant to %s", v.Value, typ.String())
if v.Value == nil {
return converr
}
switch typ.(type) {
case *godwarf.IntType:
if v.Value.Kind() != constant.Int {
return converr
}
case *godwarf.UintType:
if v.Value.Kind() != constant.Int {
return converr
}View on GitHub (pinned to a23773e6c3)
Solutions
- Compare against the zero value instead: x == 0 for ints, x == "" for strings, x == false for bools
- Only assign/convert nil to reference types: *T, map, slice, chan, func, interface
- If checking for emptiness of a struct, test a field or use a pointer to the struct and compare to nil
- Remove the invalid nil cast from breakpoint conditions/watch expressions
Example fix
// before cond: x == nil // x is int // after cond: x == 0 // zero-value comparison for int
Defensive patterns
Strategy: validation
Validate before calling
// nil is only valid for slice/map/func/ptr/chan/interface // for other types compare to zero value: cond: x == 0 // int cond: s == "" // string
Type guard
// only use nil when the target type is a reference type
// func isNilable(typ string) bool { return strings.Contains(typ, "*") || typ=="map" || typ=="slice" || typ=="chan" || typ=="func" } Prevention
- Never cast nil to value types (int, string, struct, bool)
- Compare value types to their zero values instead of nil
- Use pointers for structs that need nil-ability
When it happens
Trigger: Evaluating expressions like (*T)(nil) on wrong types: e.g. int(nil), string(nil), or struct-typed conversions from nil in the debugger console or breakpoint conditions, hitting the nilVariable check in convertToType.
Common situations: Typing nil where Go itself would reject it; autogenerated conditions comparing a non-reference variable to nil (e.g. myInt == nil converted via type assertion); ported code assuming nil converts to any zero value.
Related errors
- variable to reslice is not an array, slice, or map
- invalid argument 1 %s (type %s) to complex
- invalid argument 2 %s (type %s) to complex
- invalid argument %s (type %s) to imag
- invalid argument %s (type %s) to real
AI-assisted analysis of go-delve/delve@a23773e6c3 (2026-08-31).
Data as JSON: /api/errors/bb6234e4353bb1c0.
Report an issue: GitHub.