go-delve/delve · error
can not assign to %s
Error message
can not assign to %s
What it means
setValue cannot assign to a destination of kind Func whose FuncType size is 0: function values in Go are not assignable from the debugger. When the destination variable has a name it is reported via this error; anonymous function destinations get the sibling 'can not assign to function expression' error.
Source
Thrown at pkg/proc/eval.go:658
case reflect.Float32, reflect.Float64:
f, _ := constant.Float64Val(srcv.Value)
return dstv.writeFloatRaw(f, dstv.RealType.Size())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n, _ := constant.Int64Val(srcv.Value)
return dstv.writeUint(uint64(n), dstv.RealType.Size())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
n, _ := constant.Uint64Val(srcv.Value)
return dstv.writeUint(n, dstv.RealType.Size())
case reflect.Bool:
return dstv.writeBool(constant.BoolVal(srcv.Value))
case reflect.Complex64, reflect.Complex128:
real, _ := constant.Float64Val(constant.Real(srcv.Value))
imag, _ := constant.Float64Val(constant.Imag(srcv.Value))
return dstv.writeComplex(real, imag, dstv.RealType.Size())
case reflect.Func:
if dstv.RealType.Size() == 0 {
if dstv.Name != "" {
return fmt.Errorf("can not assign to %s", dstv.Name)
}
return errors.New("can not assign to function expression")
}
}
// nilling nillable variables
if srcv == nilVariable {
return dstv.writeZero()
}
if srcv.Kind == reflect.String {
if srcv.Base == 0 && srcv.Len > 0 && srcv.Flags&VariableConstant != 0 {
return errFuncCallNotAllowedStrAlloc
}
return dstv.writeString(uint64(srcv.Len), srcv.Base)
}
// slice assignment (this is not handled by the writeCopy below so thatView on GitHub (pinned to a23773e6c3)
Solutions
- Do not assign to function values — the Go debugger cannot rewrite function values; assign an indirect holder instead.
- If the goal is indirection, store the target in a func-typed field/variable you control and assign that field's underlying state, or wrap the call: set a bool flag the code checks instead of swapping the func.
- If this came from call-injection argument copying, change the injected function signature to not take/return a func argument at that position.
- Restrict the UI/automation to disallow assignments whose destination kind is Func before invoking SetVariable.
Example fix
// before
scope.SetVariable("handler", "altHandler") // can not assign to handler
// after
// introduce a selectable wrapper instead of assigning the func value
scope.SetVariable("useAltHandler", "true") // code: if useAltHandler { handler = altHandler } Defensive patterns
Strategy: type-guard
Validate before calling
dst, err := scope.EvalExpression(name, loadSingleValue)
if err != nil { return err }
if dst.Kind == reflect.Func {
return fmt.Errorf("%s is a function value and cannot be assigned", name)
} Type guard
func isAssignableKind(k reflect.Kind) bool {
return k != reflect.Func && k != reflect.UnsafePointer
} Try / catch
if err := scope.SetVariable(name, value); err != nil {
if strings.HasPrefix(err.Error(), "can not assign to ") {
return fmt.Errorf("destination %q is a function value: assignment unsupported", name)
}
return err
} Prevention
- Never script assignments whose target names a function symbol
- Filter editable targets by Kind before exposing assignment in tooling
- Use behavioral indirection (flags, config variables) instead of swapping func values
- Check the destination variable's Kind in a prior eval before issuing SetVariable
When it happens
Trigger: SetVariable("someFunc", ...) or a call-injection argument copy where the target name/expression resolves to a function value (dstv.Kind == reflect.Func with RealType.Size() == 0); e.g. trying to overwrite a function variable, or passing a function-valued destination during fncall argument setup.
Common situations: Attempting `set f = otherFunc` on a func-typed variable; trying to patch a function pointer/callback field; IDE users dragging a function symbol into an assignment; trying to swap implementations at runtime (not supported).
Related errors
- Expression %q is unreadable: %v
- can not set variables of type %s (not implemented)
- could not decode first frame
- unable to find function context
- unable to find locals: no debug information present in binar
AI-assisted analysis of go-delve/delve@a23773e6c3 (2026-08-31).
Data as JSON: /api/errors/7bac9c01473d4568.
Report an issue: GitHub.