GoLang上的反射错误 - 参数太少 [英] Reflection error on GoLang - Too few arguments
问题描述
我有这个控制器:
包裹网站
导入(
网络/ http
)
func init(){
}
func(controller * Controller)Index(r * http。 Request)(string,int){
returnTesting,http.StatusOK
}
使用这个处理函数:
type Application struct {
}
func (application * Application)Route(控制器接口{},路由字符串)http.HandlerFunc {
return func(w http.ResponseWriter,r * http.Request){
var ptr reflect。值
var value reflect.Value
var finalMethod reflect.Value
value = reflect.ValueOf(控制器)
//如果我们以指针,我们需要将值指向
//如果我们以一个值开始,我们需要得到一个指向该值的指针
if value.Type()。Kind()== reflect.Ptr {
ptr =价值
v alue = ptr.Elem()
} else {
ptr = reflect.New(reflect.TypeOf(controller))
temp:= ptr.Elem()
temp.Set (值)
}
//检查方法的值
method:= value.MethodByName(route)
if method.IsValid(){
finalMethod = method
}
//检查指针的方法
method = ptr.MethodByName(route)
if method.IsValid(){
finalMethod = method
methodInterface:= finalMethod.Call([] reflect.Value {})[0] .Interface()
method_route:= methodInterface。(func(r * http ($)$($ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'
case http.StatusSeeOther,http.StatusFound:
http.Redirect(w,r,body,code)
默认值:
w.WriteHeader(code)
io.WriteString(w,body)
}
}
}
它以这种方式执行:
控制器:=& web .Controller {}
application:=& system.Application {}
http.HandleFunc(/,application.Route(controller,Index))
问题是编译好。它没有显示任何错误,但是当我访问网站时,只需指向localhost即可显示:
2014 / 12/27 22:38:16 http:panic serve 127.0.0.1:58304:reflect:调用输入参数太少
goroutine 20 [running]:
net / http.func·011()
/usr/local/Cellar/go/1.3.3/libexec/src/pkg/net/http/server.go:1100 + 0xb7
我找不到任何错误,并且它编译的更加奇怪OK ...我是Go的新手,所以我不知道发生了什么...
我已经通过阅读找到了答案: $ b
https://stackoverflow.com/a/20715067/1339973
因此,而不是试图调用该方法:
methodInterface:= finalMethod.Call([] reflect.Value {})[0 ] .Interface()
method_route:= methodInterface。(func(r * http.Request)(string,int))
body,code:= metho d_route(r)
我只需要获取我需要的接口,然后将其转换为函数并调用它因此。
methodInterface:= finalMethod.Interface()
method_route:= methodInterface。(func(r * http.Request) (string,int))
body,code:= method_route(r)
实际上,这是我已经在做的事情,但方式不对。
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...
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.
这篇关于GoLang上的反射错误 - 参数太少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!