当前位置:网站首页>Gin framework learning code
Gin framework learning code
2022-07-02 04:30:00 【L 00】
One . Routing group requests
package main
import (
"fmt"
"github.com/gin-gonic/gin"
)
func main() {
engine := gin.Default()
routeGroup := engine.Group("/user")
routeGroup.POST("/register", registerHandle)
routeGroup.POST("login", loginHandle)
routeGroup.DELETE("/:id", deleteHandle)
engine.Run()
}
func registerHandle(context *gin.Context) {
fullPath := " The user login " + context.FullPath()
fmt.Println(context.FullPath())
context.Writer.WriteString(fullPath)
}
func loginHandle(context *gin.Context) {
fullPath := " The user login " + context.FullPath()
fmt.Println(context.FullPath())
context.Writer.WriteString(fullPath)
}
func deleteHandle(context *gin.Context) {
fullPath := " User deletion " + context.FullPath()
userID := context.Param("id")
fmt.Println(context.FullPath() + userID)
context.Writer.WriteString(fullPath + " " + userID)
}
result :

Two . middleware
Middleware is between the server segment and the specific business logic processing , The contents include : Authority handling , Certification processing, etc .
Gin Middleware :
package main
import (
"fmt"
"github.com/gin-gonic/gin"
)
func main() {
engine := gin.Default()
engine.GET("/query", Request(), func(context *gin.Context) {
context.JSON(404, map[string]interface{
}{
"code": 1,
"msp": context.FullPath(),
})
})
engine.Run(":9000")
}
func Request() gin.HandlerFunc {
return func(context *gin.Context) {
path := context.FullPath()
method := context.Request.Method
fmt.Println("path request :", path)
fmt.Println("method request :", method)
fmt.Println(" Status code :", context.Writer.Status())
fmt.Println()
context.Next()
fmt.Println(" Status code :", context.Writer.Status())
}
}
context.Next() In effect : Divide the code into two sections , We will execute first , The latter will wait until the specific code is executed .
result :
边栏推荐
- 深圳打造全球“鸿蒙欧拉之城”将加快培育生态,优秀项目最高资助 1000 万元
- 云服务器的安全设置常识
- The solution to the complexity brought by lambda expression
- FAQ | FAQ for building applications for large screen devices
- Pytorch---使用Pytorch进行鸟类的预测
- Installation et utilisation du lac bleu
- 阿里云polkit pkexec 本地提权漏洞
- 66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)
- 汇编语言中的标志位:CF、PF、AF、ZF、SF、TF、IF、DF、OF
- Wechat applet - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
猜你喜欢

Lei Jun wrote a blog when he was a programmer. It's awesome

The solution to the complexity brought by lambda expression

文档声明与字符编码

How much can a job hopping increase? Today, I saw the ceiling of job hopping.

Introduction to vmware workstation and vSphere

Pytorch---使用Pytorch实现U-Net进行语义分割

WiFi 5GHz frequency

Actual combat | use composite material 3 in application

PIP installation of third-party libraries

Force buckle 540 A single element in an ordered array
随机推荐
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
Pytoch yolov5 runs bug solution from 0:
社交媒体搜索引擎优化及其重要性
Pytoch --- use pytoch to predict birds
GeoTrust ov multi domain SSL certificate is 2100 yuan a year. How many domain names does it contain?
[C language] Dynamic Planning --- from entry to standing up
Go function
Thinkphp6 limit interface access frequency
June book news | 9 new books are listed, with a strong lineup and eyes closed!
Pytorch---使用Pytorch进行图像定位
Wechat applet calculates the distance between the two places
Pytorch---使用Pytorch进行鸟类的预测
office_ Delete the last page of word (the seemingly blank page)
Shutdown procedure after 60
Feature Engineering: summary of common feature transformation methods
Wechat applet JWT login issue token
Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
I sorted out some basic questions about opencv AI kit.
Pytoch --- use pytoch for image positioning
Yyds dry goods inventory kubernetes introduction foundation pod concept and related operations