dongtan7418 2016-08-17 02:57
浏览 53
已采纳

Iris框架在实现指针接收器和值接收器方面的差异

I am studying Iris framework recently. I encountered a question when I was implementing the Handler. Like following:

package controller

import "github.com/kataras/iris"    

type Pages struct{
}

func (p *Pages) Serve(c *iris.Context) {
}

In order to use this controller, I implemented the following entry script:

package main

import (
     "github.com/kataras/iris"
     "web/controller"
)

func main(){
      ctrl := controller.Pages{}
      iris.Handle("GET", "/", ctrl)
      iris.Listen(":8080")
}

But when I compiled the code, I got the following error message:

cannot use ctrl (type controllers.Pages) as type iris.Handler in argument to iris.Handle:
controllers.Pages does not implement iris.Handler (Serve method has pointer receiver)

After I changed the declaration to:

ctrl := &controller.Pages{}

Then the compiler passed with no complaint.

The question is: I thought the following statements are equal, since the GO compiler will do the conversion under the table:

type Pages struct {
}

func (p *Pages) goWithPointer() {
    fmt.Println("goWithPointer")
}

func (p Pages) goWithValue() {
    fmt.Println("goWithValue")
}

func main() {
    p1 := Pages{}
    p2 := &Pages{}

    p1.goWithPointer()
    p1.goWithValue()

    p2.goWithPointer()
    p2.goWithValue()
}

Why can't I use ctrl := controller.Pages{} as the parameter to iris.Handle(), instead of ctrl := &controller.Pages{} as the parameter to iris.Handle()?

Thank you for your time and sharing.

  • 写回答

2条回答 默认 最新

  • dpbyr64224 2016-08-17 03:27
    关注

    See Docs:

    A type may have a method set associated with it. The method set of an interface type is its interface. The method set of any other type T consists of all methods declared with receiver type T. The method set of the corresponding pointer type *T is the set of all methods declared with receiver *T or T (that is, it also contains the method set of T). Further rules apply to structs containing anonymous fields, as described in the section on struct types. Any other type has an empty method set. In a method set, each method must have a unique non-blank method name.

    And see: https://stackoverflow.com/a/33591156/6169399 :

    If you have an interface I, and some or all of the methods in I's method set are provided by methods with a receiver of *T (with the remainder being provided by methods with a receiver of T), then *T satisfies the interface I, but T doesn't. That is because *T's method set includes T's, but not the other way around.

    Using ctrl := Pages{} makes error:

    cannot use ctrl (type Pages) as type Handler in argument to Handle:
    Pages does not implement Handler (Serve method has pointer receiver)
    

    Using ctrl := Pages{}needs:

    func (p Pages) Serve() {
        fmt.Println(p.i)
    }
    

    Iris Handler is an interface type. like this working sample (see comment):

    package main
    
    import "fmt"
    
    type Handler interface {
        Serve()
    }
    
    type Pages struct {
        i int
    }
    
    func (p *Pages) Serve() {
        fmt.Println(p.i)
    }
    
    func Handle(p Handler) {
        p.Serve()
    }
    
    func main() {
        // cannot use ctrl (type Pages) as type Handler in argument to Handle:
        // Pages does not implement Handler (Serve method has pointer receiver)
        //ctrl := Pages{}
        ctrl := &Pages{101}
        Handle(ctrl)
    }
    

    output:

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

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题