简体   繁体   English

GoLang的反思错误 - 参数太少

[英]Reflection error on GoLang - Too few arguments

I have this controller: 我有这个控制器:

package web

import (
    "net/http"
)

func init() {

}

func (controller *Controller) Index(r *http.Request) (string, int) {
    return "Testing", http.StatusOK
}

With this handler: 使用此处理程序:

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

And it is executed in this way: 它以这种方式执行:

controller := &web.Controller{}
application := &system.Application{}

http.HandleFunc("/", application.Route(controller, "Index"))

The problem is it compiled ok. 问题是编译好了。 It does not show any error, but when I go to the website, just by pointing at localhost, it shows: 它没有显示任何错误,但是当我访问网站时,只需指向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

I cannot find any error, and it is more strange it compiles ok... I'm new in Go, so I've no idea what is going on... 我找不到任何错误,更奇怪的是它编译好了......我是Go的新手,所以我不知道发生了什么......

I have found the answer by reading this: 我通过阅读这个找到了答案:

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

So instead of trying to call the method: 所以不要试图调用方法:

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

I just get the interface I need, then convert it into a function and call it as such. 我只是获得了我需要的接口,然后将其转换为函数并将其调用。

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

Actually, that is kind of what I was already doing, but in the wrong way. 实际上,这就是我已经在做的事情,但方式错误。

As explained in " How to properly use .Call in reflect package, Golang? ", and in reflect#Value.Call() , you need a slice with at least 1 element of the right type in it, if your function takes one parameter. 如“ 如何正确使用.Call in reflect package,Golang? ”和reflect#Value.Call()中所述,如果函数有一个参数,则需要一个至少包含1个正确类型元素的切片。 。

If you know the exact parameter type and value, you need to create it, and build your Call parameter: 如果您知道确切的参数类型和值,则需要创建它,并构建Call参数:

 in := []reflect.Value{reflect.ValueOf(m)}

The exception " reflect: Call with too few input arguments " is called after checking the number of parameters expected by the function 检查函数预期的参数数量之后, reflect: Call with too few input arguments异常“ reflect: Call with too few input arguments

NumIn returns a function type's input parameter count. NumIn返回函数类型的输入参数计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM