This is a creation in Article, where the information may have evolved or changed.
Statement
This paper mainly records I in the use of IRIS Framework MVC module in the process of causing controller reflection error, for reference only. If not included in the situation is normal.
The controller has a reflection error and can only see the exception type in the exception information, but no specific error code is found in the exception stack. Debugging is quite difficult, your own code is good, if someone took the code to find you, don't mention how much pain. So record the situation encountered, if you encounter similar errors, retrace, troubleshooting problems.
Objective
Iris, the _example folder in its GitHub repository can find examples, but no documentation is used. To be familiar with the frame, climb the code honestly (╯▔ dish ▔) ╯
Introduction to MVC pattern
~blablabla~
Iris MVC Introduction
~blablabla~
Controller Reflection Error
Controller and Reflection
In non-singleton mode, the MVC module instantiates a controller for each request (incorrect). This will be from through MVC. New () copies a copy of the controller instance registered to the app. The Golang reflect module is used for specific types of structs .
The situation I met
struct definitions use private member variables
type MyController struct{ repositroy mydto.Repositroy Ctx iris.Context}
The above Repositroy is used for database operations, because it is a private member, causing the reflection to be unable to instantiate the variable, resulting in a reflection exception, the nil point error is reported.
Specific handler method parameter entry error
The controller's handler method definition is not prone to errors (in a few cases anyway), but I have made the mistake of entering the parameter. Look at the code
func (c *MyController) Get(ip sting) error{ ···}
The controller's handler method does not have an entry parameter by default. All methods that need to be joined must be in beforeactivation (MVC. beforeactivation) {} is registered.
func (c *MyController) BeforeActivation(b mvc.BeforeActivation){ b.Handle("GET","/{ip:string}","Get")}
Without this step, a reflect error occurs. For JSON parameters, the defined Ctx variable can be used, and the frame is injected into the corresponding context when reflected.