dongqiao8421 2017-01-17 01:26
浏览 32
已采纳

为接受接口的函数传递结构

I have the following code:

package main


type MyInterface interface {
    Test()
}

type MyType struct {

}
func (m MyType) Test(){}

func AcceptInterface(i *MyInterface){
}

func main() {

    object := &MyType{}
    AcceptInterface(object)
}

I was expecting this to work, because MyType implements MyInterface, but I get:

cannot use object (type *MyType) as type *MyInterface in argument to AcceptInterface: *MyInterface is pointer to interface, not interface

I tried doing type assertion: object.(MyInterface), but that doesn't work either.

How can I accomplish this?

  • 写回答

3条回答 默认 最新

  • 普通网友 2017-01-17 02:04
    关注

    As the error says,

    cannot use object (type *MyType) as type *MyInterface in argument to AcceptInterface: *MyInterface is pointer to interface, not interface

    This means that it is expecting an interface value, not a pointer.

    If you change the pointers to values in your code (by removing the & and *), the program will run with no errors:

    package main
    
    
    type MyInterface interface {
        Test()
    }
    
    type MyType struct {
    
    }
    func (m MyType) Test(){}
    
    func AcceptInterface(i MyInterface){
    }
    
    func main() {
    
        object := MyType{}
        AcceptInterface(object)
    }
    

    Play it

    Edit 1

    If you still want to use a pointer as an argument, there are two important parts of the Go language to note

    1. From the Go Spec on what exacly is a variable that fits an instance:

      A variable of interface type can store a value of any type with a method set that is any superset of the interface.

    2. From the Go Spec on what pointers being automatically dereferenced:

      As with selectors, a reference to a non-interface method with a value receiver using a pointer will automatically dereference that pointer: pt.Mv is equivalent to (*pt).Mv [and] as with method calls, a reference to a non-interface method with a pointer receiver using an addressable value will automatically take the address of that value: t.Mp is equivalent to (&t).Mp.

    Those two points are important, because when combined they explain that pointers to variables can still fit instances. This is because the pointer's method set is automatically dereferenced by the Go compiler (and since the variable it is referencing can fit an instance, the pointer can, too)!

    In action, this means that in order to see if a pointer fits an instance, you have to declare the instance as a value and the pointer as a pointer.

    If you run this code:

    package main
    
    type MyInterface interface {
        Test()
    }
    
    type MyType struct {
    }
    
    func (m MyType) Test() {}
    
    func AcceptInterface(i MyInterface) {
    }
    
    func main() {
        object := &MyType{}
        AcceptInterface(object)
    }
    

    Play it

    you will see that there are no errors! Notice how there is an & in the object declaration, but no * in the i declaration?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看