英文:
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.Println
s 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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论