Golang-为什么在ServeHTTP函数中发生此错误:反射:反射的调用.Value.调用零值



我的代码和错误消息在这里:https://gist.github.com/WithGJR/a700e5d5bd35b5c8eef2

有人能为我解释一下为什么会发生这个错误以及如何修复它吗?谢谢

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

最新更新