本文介绍了serveHTTP实用程序从何而来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个实用程序:

type Handler struct{}

func (h Handler) Mount(router *mux.Router, v PeopleInjection) {
    router.HandleFunc("/api/v1/people", h.makeGetMany(v)).Methods("GET")
}

上面这样称呼:

func (h Handler) makeGetMany(v PeopleInjection) http.HandlerFunc {

    type RespBody struct {}

    type ReqBody struct {
        Handle string
    }

    return tc.ExtractType(
        tc.TypeList{ReqBody{},RespBody{}},
        func(w http.ResponseWriter, r *http.Request) {
         // ...
    })
}

然后是 tc.ExtractType 就像这样:

func ExtractType(s TypeList, h http.HandlerFunc) http.HandlerFunc {

    return func(w http.ResponseWriter, r *http.Request) {

        h.ServeHTTP(w, r)  // <<< h is just a func right? so where does ServeHTTP come from?
    }
}

我的问题是-serveHTTP方法/功能来自哪里?

my question is - where does the serveHTTP method/func come from??

不仅仅是 h 参数,而是具有以下签名的函子:

isn't the h parameter just a func with this signature:

func(w http.ResponseWriter, r *http.Request) { ... }

那么该功能将如何附加 ServeHTTP 功能?

so then how does that func have the ServeHTTP func attached to it?

换句话说,我为什么打电话

In other words, why I am I calling

h.ServeHTTP(w,r)

代替

h(w,r)

?

推荐答案

http.HandlerFunc是代表 func(ResponseWriter,* Request)的类型.

http.HandlerFunc func(ResponseWriter,* Request)之间的区别是: http.HandlerFunc 类型的方法称为ServeHTTP().

The difference between http.HandlerFunc and func(ResponseWriter, *Request) is: http.HandlerFunc type has method called ServeHTTP().

源代码:

// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as HTTP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler that calls f.
type HandlerFunc func(ResponseWriter, *Request)

// ServeHTTP calls f(w, r).
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
    f(w, r)
}


http.HandlerFunc()可用于包装处理程序函数.


The http.HandlerFunc() can be used to wrap handler function.

func Something(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        // do something

        next.ServeHTTP(w, r)
    })
}

包装的处理程序将具有 http.HandlerFunc()类型,这意味着我们将能够访问它的 .ServeHTTP()方法.

The wrapped handler will have http.HandlerFunc() type, meaning we'll be able to access it's .ServeHTTP() method.

还有另一种类型,称为 http.Handler 的接口.它具有 .ServeHTTP()方法签名,并且必须在嵌入接口的结构上实现.

There is also another type, an interface called http.Handler. It has .ServeHTTP() method signature, and it must be implemented on the struct where the interface is being embedded.

type Handler interface {
    ServeHTTP(ResponseWriter, *Request)
}

正如您在上面的 Something()函数上所看到的那样,必须使用 http.Handler 类型的返回值,但是我们返回了包装在 http中的处理程序.HandlerFunc().很好,因为 http.HandlerFunc 具有方法 .ServeHTTP()可以满足 http.Handler 接口的要求.

As you can see on Something() function above, a return value in http.Handler type is required, but we returned a handler wrapped in http.HandlerFunc(). It's fine, because the http.HandlerFunc has method .ServeHTTP() who fulfil the requirement of http.Handler interface.

由于要继续处理传入的请求,您需要调用 .ServeHTTP().

Because to continue serve the incoming request, you need to call .ServeHTTP().

这篇关于serveHTTP实用程序从何而来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-23 22:51