【问题标题】:Reflection error on GoLang - Too few argumentsGoLang 上的反射错误 - 参数太少
【发布时间】:2014-12-30 00:53:37
【问题描述】:

我有这个控制器:

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
        }

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

问题是编译正常。它没有显示任何错误,但是当我访问该网站时,只需指向本地主机,它就会显示:

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 新手,所以我不知道发生了什么...

【问题讨论】:

    标签: go routes


    【解决方案1】:

    我通过阅读找到了答案:

    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, int))
        body, code := method_route(r)
    

    实际上,这就是我已经在做的事情,但是方法不对。

    【讨论】:

    • 类型断言的有趣使用。 +1。比我的回答更准确。
    【解决方案2】:

    如“How to properly use .Call in reflect package, Golang?”和reflect#Value.Call() 中所述,如果您的函数采用一个参数,则您需要一个包含至少 1 个正确类型元素的切片。

    如果您知道确切的参数类型和值,则需要创建它,并构建您的调用参数:

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

    checking the number of parameters expected by the function之后调用异常“reflect: Call with too few input arguments

    NumIn 返回函数类型的输入参数计数。

    【讨论】:

    • 非常感谢您的帮助。正如您在我的问题中所读到的,我是新手。所以我得到了你的答案。在代码中你可以看到它接收的参数是一个http请求,所以每个请求都会不同。如何在通话中构建它?再次感谢!
    • @Cito 不确定,考虑到您需要为每个函数Call() 传递正确的参数列表。除非函数名称足以让您猜测要传递的正确值,否则这可能是个问题。
    • 我已经这样做了:methodInterface := finalMethod.Call([]reflect.Value{reflect.ValueOf(r)})[0].Interface() 但现在错误是:interface conversion: interface is string, not func(*http.Request) (string, int)
    • 感谢您的提问。你为我指明了正确的方向。通过阅读这个问题:stackoverflow.com/a/20715067/1339973 我已经能够修复它。
    猜你喜欢
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 2018-04-22
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    相关资源
    最近更新 更多