-2

My problem is that I need a pointer to an element of a list asserted as type T:

e *list.Element

t *T

But if I want to use type assertion, I can only get a copy of e.Value:

if instance, ok := e.Value.(T); ok {
    t = &instance
}

How do I get the reference to e.Value as a pointer to type T?

blackgreen
  • 34,072
  • 23
  • 111
  • 129
just a guy
  • 137
  • 7
  • 4
    If a value of type `T` is stored, you can't. Store `*T` values, so you can type assert `*T` from `e`. – icza Nov 11 '20 at 19:10

1 Answers1

0

If e stores a value of type T, you can't.

Store *T values in the elements, so you can type assert *T from e.

See related questions:

Get pointer on var obtained via interface

Cannot take the address of map element

How can I store reference to the result of an operation in Go?

How to get the pointer of return value from function call?

icza
  • 389,944
  • 63
  • 907
  • 827