Golang – Why this error happened in ServeHTTP Function : reflect: call of reflect.Value.Call on zero Value

huangapple go评论76阅读模式
英文:

Golang - Why this error happened in ServeHTTP Function : reflect: call of reflect.Value.Call on zero Value

问题

这是我的代码和错误信息:https://gist.github.com/WithGJR/a700e5d5bd35b5c8eef2

有人可以解释一下为什么会出现这个错误,以及如何修复吗?谢谢。

英文:

My code and error message are in here: https://gist.github.com/WithGJR/a700e5d5bd35b5c8eef2

Could anyone explain for me why this error occured and how to fix it? Thanks.

答案1

得分: 0

因为value.MethodByName(info.controllerMethodName)可能返回一个无效的方法,所以你应该检查method.IsValid()

当出现这种情况时,你可以开始添加一堆log.Println来查看发生了什么,直到引入一个合适的调试器为止。

//编辑

router.Get("/", controllers.IndexController{}, "Index")

你正在传递一个值,func (this *IndexController) Index()是在指针上定义的,所以你的MethodByName不起作用,将你的router.Get更改为:

router.Get("/", &controllers.IndexController{}, "Index")
英文:

Because value.MethodByName(info.controllerMethodName) probably returns an invalid method, you should check method.IsValid().

When something like this happens you start adding a bunch of log.Printlns to see what's happening, well until a proper debugger is introduced.

//edit

router.Get("/", controllers.IndexController{}, "Index")

You're passing a value, func (this *IndexController) Index() is defined on the pointer so your MethodByName isn't working right, change your router.Get to :

router.Get("/", &controllers.IndexController{}, "Index")

huangapple
  • 本文由 发表于 2014年9月2日 11:29:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/25615397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定