dotdx80642 2019-06-18 15:51
浏览 227
已采纳

在Go中使用自定义http.Handler时为什么要使用指针?

When calling http.Handle() in the code snippet below, I'm using my own templateHandler type which implements the http.Handler interface.

package main

import (
    "html/template"
    "log"
    "net/http"
    "path/filepath"
    "sync"
)

type templateHandler struct {
    once     sync.Once
    filename string
    templ    *template.Template
}

func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    t.once.Do(func() {
        t.templ = template.Must(template.ParseFiles(filepath.Join("templates", t.filename)))
    })
    t.templ.Execute(w, nil)
}

func main() {
    http.Handle("/", &templateHandler{filename: "chat.html"})
    if err := http.ListenAndServe(":8080", nil); err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

Now for some reason I have to pass a pointer to http.Handle() using &templateHandler{filename: "chat.html"}. Without the & I get the following error:

cannot use (templateHandler literal) (value of type templateHandler) 
as http.Handler value in argument to http.Handle: 
missing method ServeHTTP

Why exactly is this happening? What difference does using a pointer make in this case?

  • 写回答

1条回答 默认 最新

  • douyanguo7964 2019-06-18 15:59
    关注

    http.Handle() expects a value (any value) that implements http.Handler, which means it must have a ServeHTTP() method.

    You used pointer receiver for the templateHandler.ServeHTTP() method, which means only a pointer value to templateHandler has this method, but not that of a non-pointer templateHandler type.

    Spec: Method sets:

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

    A non-pointer type only has methods with non-pointer receivers. A pointer type has methods both with pointer and non-pointer receivers.

    Your ServeHTTP() method modifies the receiver, so it must be a pointer. But if some other handler does not need to, the ServeHTTP() method may be created using a non-pointer receiver, and in that case you can use a non-pointer value as the http.Handler, like in this example:

    type myhandler struct{}
    
    func (m myhandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {}
    
    func main() {
        // non-pointer struct value implements http.Handler:
        http.Handle("/", myhandler{})
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分