GoLang的反思错误 – 参数太少

GoLang的反思错误 – 参数太少,第1张

概述我有这个控制器: package webimport ( "net/http")func init() {}func (controller *Controller) Index(r *http.Request) (string, int) { return "Testing", http.StatusOK} 使用此处理程序: type Application 我有这个控制器:

package webimport (    "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        }        methodInterface := finalMethod.Call([]reflect.Value{})[0].Interface()        method_route := methodInterface.(func(r *http.Request) (string,int))        body,code := method_route(r)        switch code {        case http.StatusOK:            io.WriteString(w,body)        case http.StatusSeeOther,http.StatusFound:            http.Redirect(w,r,body,code)        default:            w.Writeheader(code)            io.WriteString(w,body)        }    }}

它以这种方式执行:

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 argumentsgoroutine 20 [running]:net/http.func·011()    /usr/local/Cellar/go/1.3.3/libexec/src/pkg/net/http/server.go:1100 +0xb7

我找不到任何错误,更奇怪的是它编译好了……我是Go的新手,所以我不知道发生了什么……

解决方法 我通过阅读这个找到了答案:

https://stackoverflow.com/a/20715067/1339973

所以不要试图调用方法:

methodInterface := finalMethod.Call([]reflect.Value{})[0].Interface()    method_route := methodInterface.(func(r *http.Request) (string,int))    body,code := method_route(r)

我只是获得了我需要的接口,然后将其转换为函数并将其调用.

methodInterface := finalMethod.Interface()    method_route := methodInterface.(func(r *http.Request) (string,code := method_route(r)

实际上,这就是我已经在做的事情,但方式错误.

总结

以上是内存溢出为你收集整理的GoLang的反思错误 – 参数太少全部内容,希望文章能够帮你解决GoLang的反思错误 – 参数太少所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1266282.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存