When you initiate the Gin framework, the related init() methods in the package will be executed; the following two init methods are found:
a. The first init method
b. The second init method
gin.Default()
The source code of gin.Default is as follows:
Middleware is registered in the Gin framework through engine.
engine.GET(“/”,…)
"/" In the above example code, we registered a route that matches the root directory ( ), the processing handlers is an anonymous function, and the data in the ctx.JSON returned json format is called directly.
Multiple return formats
In the gin.Context support of multiple return formats, the commonly used return formats are as follows:
Method Name
Describe
ctx.XML(code int, obj interface{})
return xml
ctx.AsciiJSON(code int, obj interface{})
return json, will encode special characters
ctx.PureJSON(code int, obj interface{})
returns json, and html some are not escaped.
Comparison of Json, AsciiJSON, PureJSON
The request returns:
Summarize
Method
Phenomenon
ctx.JSON
By default, html will be converted into unicode characters, and no additional processing will be performed on special characters.
ctx.PureJSON
html returned as it is, and no additional processing will be done to special characters.
ctx.AsciiJSON
Special characters and html processed together.
engine.Run()
The source code is as follows
Why the default monitor is “:9090”
Run Called in resolveAddress(addr) the method, the source code of the method is as follows: