dongyong6428 2014-12-28 04:42
浏览 122
已采纳

GoLang上的反射错误-参数太少

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:

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...

  • 写回答

2条回答 默认 最新

  • doudengshen5591 2014-12-29 16:53
    关注

    I have found the answer by reading this:

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀