user6216224
user6216224

Reputation:

How to obtain pointer from reflect.Value in Go?

As far as I know methods with pointer receivers only implement interfaces for pointers. I want to utilize the encoding.TextMarshaler interface which must be implmented with a pointer. How can I therefore obtain a pointer using reflection.

The following does not work because as mentionend a value does not implement the interface:

v.Interface().(encoding.TextUnmarshaler)

Upvotes: 1

Views: 96

Answers (1)

Thundercat
Thundercat

Reputation: 121129

Use v.Addr().Interface().(encoding.TextUnmarshaler) to get the address. This assumes that v references an addressable value.

Upvotes: 2

Related Questions