【问题标题】:How to obtain pointer from reflect.Value in Go?如何从 Go 中的 reflect.Value 获取指针?
【发布时间】:2018-06-14 16:26:14
【问题描述】:

据我所知,带有指针接收器的方法只实现指针接口。我想利用必须用指针实现的encoding.TextMarshaler 接口。因此,我怎样才能使用反射获得指针。

以下不起作用,因为作为提到的值没有实现接口:

v.Interface().(encoding.TextUnmarshaler)

【问题讨论】:

标签: pointers go reflection interface


【解决方案1】:

使用 v.Addr().Interface().(encoding.TextUnmarshaler) 获取地址。这假定 v 引用了一个 addressable 值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-05
    • 2021-01-21
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 2021-01-07
    • 2017-07-17
    相关资源
    最近更新 更多