[Ginny series] Go-based web background development, tool functions, error handling and middleware

Utility functions, error handling, and middleware

Now you will find that you can write a large number of interfaces easily and freely: you can even write hundreds of interfaces a day if you want!

But when you build your application brutally like this, you always feel a little uncomfortable: you have to write a lot of sums, c.JSON()and gin.H{}almost always check at BadRequestthe beginning of each time . Although it is actually an error, we just returned. It's just a string.

In the next few subsections, we will make a little bit of 重构our code again .

4.1 Utility functions: reuse your code snippets

First of all, we need to know what our first purpose is: so that we don’t have to handlerwrite inc.JSON()

Guess you like

Origin blog.csdn.net/weixin_54707168/article/details/114779924