我有这个控制器:
package web
import (
"net/http"
)
func init() {
}
func (controller *Controller) Index(r *http.Request) (string, int) {
return "Testing", http.StatusOK
}
使用此处理程序:
type Application struct {
}
func (application *Application) Route(controller interface{}, route string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var ptr reflect.Value
var value reflect.Value
var finalMethod reflect.Value
value = reflect.ValueOf(controller)
// if we start with a pointer, we need to get value pointed to
// if we start with a value, we need to get a pointer to that value
if value.Type().Kind() == reflect.Ptr {
ptr = value
value = ptr.Elem()
} else {
ptr = reflect.New(reflect.TypeOf(controller))
temp := ptr.Elem()
temp.Set(value)
}
// check for method on value
method := value.MethodByName(route)
if method.IsValid() {
finalMethod = method
}
// check for method on pointer
method = ptr.MethodByName(route)
if method.IsValid() {
finalMethod = method
}
}
}
它是这样执行的:
controller := &web.Controller{}
application := &system.Application{}
http.HandleFunc("/", application.Route(controller, "Index"))
问题是它编译好了。它没有显示任何错误,但是当我访问该网站时,只需指向 localhost,它就会显示:
2014/12/27 22:38:16 http: panic serving 127.0.0.1:58304: reflect: Call with too few input arguments
goroutine 20 [running]:
net/http.func·011()
/usr/local/Cellar/go/1.3.3/libexec/src/pkg/net/http/server.go:1100 +0xb7
我找不到任何错误,更奇怪的是它编译正常......我是 Go 新手,所以我不知道发生了什么......
相关分类