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条)

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗