http.Handle(Handler或HandlerFunc)

时间:2011-06-15 23:13:18

标签: http reflection interface go

如何实施以下功能?

func handle(pattern string, handler interface{}) {
    // ... what goes here? ...
    http.Handle(pattern, ?)
}

handle("/foo", func(w http.ResponseWriter, r http.Request) { io.WriteString(w, "foo") }
handle("/bar", BarHandler{})

handle()传递一个匹配http.HandlerFunc类型的函数或一个实现http.Handler接口的类型。

2 个答案:

答案 0 :(得分:8)

我不是采用反思,而是采取这种方式:

func handle(pattern string, handler interface{}) {
    var h http.Handler
    switch handler := handler.(type) {
    case http.Handler:
        h = handler
    case func(http.ResponseWriter, *http.Request):
        h = http.HandlerFunc(handler)
    default:
        // error
    }
    http.Handle(pattern, h)
}

答案 1 :(得分:2)

首先,我们需要在Java / C#的术语中引入术语“反射”,在C ++的术语中引入RTTI。实际上这很简单。编译器保留数据以在运行时期间找出实例var i SomeType的类型。 Go支持反射,这就是它如何在运行时找出handler的类型。

handle函数使用反射。一个粗略的例子

package main
import ("reflect";"http")
type fakeHandler struct{}
func (frw *fakeHandler) ServeHTTP(http.ResponseWriter, *http.Request) {}

func handle(pattern string, handler interface{}) {
    handlerInterface := reflect.TypeOf(new(http.Handler)).Elem()
    handlerFunction  := reflect.TypeOf(new(http.HandlerFunc)).Elem()
    t := reflect.TypeOf(handler)
    if t.Implements(handlerInterface) {fmt.Println("http.Handler")}
    //http.HandlerFunc is a different type than
    // func(http.ResponseWriter, *http.Request), but we can do
    // var hf HandlerFunc = func(http.ResponseWriter, *http.Request){}
    if t.AssignableTo(handlerFunction) {fmt.Println("http.HandleFunc")}
}
func f(http.ResponseWriter, *http.Request) {}
func main() {
    handle("",&fakeHandler{})
    handle("",f)
}