go-delve/delve · error

can not take address of %q

Error message

can not take address of %q

What it means

Thrown by evalAddrOf when evaluating &x: the operand has no address (xev.Addr == 0) or no DWARF type (xev.DwarfType == nil), so Delve cannot form a pointer to it. Values that live only in CPU registers, literals, and constants are not addressable in the debuggee's memory.

Source

Thrown at pkg/proc/eval.go:2343

		val, ok := constant.Uint64Val(xev.Value)
		if ok && val == 0 {
			stack.err = fmt.Errorf("couldn't read pointer: %w", xev.Unreadable)
			return
		}
	}
	rv := &xev.Children[0]
	if rv.Addr == 0 {
		stack.err = errors.New("nil pointer dereference")
		return
	}
	stack.push(rv)
}

// Evaluates expressions &<subexpr>
func (scope *EvalScope) evalAddrOf(op *evalop.AddrOf, stack *evalStack) {
	xev := stack.pop()
	if xev.Addr == 0 || xev.DwarfType == nil {
		stack.err = fmt.Errorf("can not take address of %q", astutil.ExprToString(op.Node.X))
		return
	}

	stack.push(xev.pointerToVariable())
}

func (v *Variable) pointerToVariable() *Variable {
	v.OnlyAddr = true

	rv := v.newVariable("", 0, godwarf.FakePointerType(v.DwarfType, int64(v.bi.Arch.PtrSize())), v.mem)
	rv.Children = []Variable{*v}
	rv.loaded = true

	return rv
}

func constantUnaryOp(op token.Token, y constant.Value) (r constant.Value, err error) {
	defer func() {

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Avoid optimized builds while debugging: compile with `-gcflags='all=-N -l'` (dlv debug does this by default; use `dlv exec` on a binary built this way) so variables stay in memory and are addressable.
  2. Assign the value to a variable in the program first, then take that variable's address.
  3. If you just need the value, print it directly instead of forming a pointer.
  4. Step to a point in the function where the variable is live in memory (after it escapes or is used addressably).

Example fix

// before (i kept in register, optimized build)
(dlv) print &i
can not take address of "&i"

// after: rebuild unoptimized
go build -gcflags='all=-N -l' -o app .
dlv exec ./app
(dlv) print &i
Defensive patterns

Strategy: validation

Validate before calling

// Only take addresses of variables with backing memory.
// In the program, ensure the value is stored in a variable:
x := 42
px := &x // valid in code; at the prompt use `print &x` only for addressable vars

Prevention

When it happens

Trigger: Evaluating `&x` where x is a constant, a literal (e.g. `&5`), a variable held entirely in a register (optimized code / register-allocated local), or a value reconstructed by Delve without a backing address. Also for temporaries like `&f()` where the function returns a value Delve did not materialize in memory.

Common situations: Trying to take the address of a small local int the compiler kept in a register in an optimized build, taking the address of a function call result at the dlv prompt, or `&someConstant`.

Related errors


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