当前位置:网站首页>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 :
边栏推荐
- How much can a job hopping increase? Today, I saw the ceiling of job hopping.
- 66.qt quick-qml自定义日历组件(支持竖屏和横屏)
- 云服务器的安全设置常识
- Mysql表insert中文变?号的问题解决办法
- Realizing deep learning framework from zero -- Introduction to neural network
- okcc为什么云呼叫中心比传统呼叫中心更好?
- geotrust ov多域名ssl证书一年两千一百元包含几个域名?
- C语言猜数字游戏
- Major domestic quantitative trading platforms
- The confusion I encountered when learning stm32
猜你喜欢

First acquaintance with P4 language

Sword finger offer II 006 Sort the sum of two numbers in the array

How much is the tuition fee of SCM training class? How long is the study time?

Federal learning: dividing non IID samples according to Dirichlet distribution

office_ Delete the last page of word (the seemingly blank page)

【c语言】动态规划---入门到起立

Play with concurrency: draw a thread state transition diagram

unable to execute xxx. SH: operation not permitted

Use a mask to restrict the input of the qlineedit control

Markdown edit syntax
随机推荐
Document declaration and character encoding
office_ Delete the last page of word (the seemingly blank page)
A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent
BGP experiment the next day
Federal learning: dividing non IID samples according to Dirichlet distribution
Typescript practice for SAP ui5
Pytoch yolov5 runs bug solution from 0:
云服务器的安全设置常识
Pytorch---使用Pytorch进行鸟类的预测
二叉树解题(一)
[improvement class] st table to solve the interval maximum value problem [2]
CorelDRAW graphics suite2022 free graphic design software
Deeply understand the concepts of synchronization and asynchrony, blocking and non blocking, parallel and serial
Is it safe to open an account with first venture securities? I like to open an account. How can I open it?
二叉树解题(二)
Introduction to vmware workstation and vSphere
[C language] basic learning notes
Let正版短信测压开源源码
uni-app - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)