小编典典

指向保存类型的接口的指针

go

解释我的问题的最短方法是该代码

var i interface{} // I can't change it. In fact this is a function,
i = Item{10}      // that receives interface{}, that contain object (not pointer to object!)

fmt.Printf("%T %v\n", i, i)
// fmt.Println(i.(NextValuer).NextVal())  // won't compile
i = &i
fmt.Printf("%T %v\n", i, i)               // there i is pointer to interface{} (not to Item)
// fmt.Println(i.(NextValuer).NextVal())  // panics
// fmt.Println(i.(*NextValuer).NextVal()) // won't compile

但是,如果我尝试将指向Item的指针设置为i,代码将起作用:

i = &Item{10}
fmt.Printf("%T %v\n", i, i)
fmt.Println(i.(NextValuer).NextVal())

但是我的函数接收对象,而不是指向它的指针。我可以得到它的类型(第一fmt.Printf)。但是当我尝试指向它的指针时,我收到的是interface{},而不是我的Object(Item)的指针。

我可以指向该对象进行调用NextVal吗?或者可能是其他方式


阅读 331

收藏
2020-07-02

共1个答案

小编典典

切勿使用指向接口的指针。如果您需要一个指针来使用指针接收器调用方法,则必须将指针放入interface{}

如果在interface{}要使用指针接收器调用方法的位置已经具有值,则需要制作该值的可寻址副本。

您想要完成i = &i的工作可能是:

item := i.(Item)
i = &item

这将创建原始副本的可寻址副本Item,然后将指向该副本的指针放入i。请注意,这永远不会更改原始值Item

如果您不知道中的类型,则可以interface{}使用“ reflect”复制值:

func nextVal(i interface{}) {
    // get the value in i
    v := reflect.ValueOf(i)

    // create a pointer to a new value of the same type as i
    n := reflect.New(v.Type())
    // set the new value with the value of i
    n.Elem().Set(v)

    // Get the new pointer as an interface, and call NextVal
    fmt.Println("NextVal:", n.Interface().(NextValuer).NextVal())

    // this could also be assigned another interface{}
    i = n.Interface()
    nv, ok := i.(NextValuer)
    fmt.Printf("i is a NextValuer: %t\nNextVal: %d\n", ok, nv.NextVal())
}

http://play.golang.org/p/gbO9QGz2Tq

2020-07-02