douxing1969
2018-01-04 21:21 阅读 369
已采纳

如何从Go中的reflect.Value获取指针?

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)
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    douliaopan1419 douliaopan1419 2018-01-04 21:25

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

    点赞 评论 复制链接分享

相关推荐