Golang Iris MVC Controller appears reflect exception

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.