当前位置:网站首页>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 :
边栏推荐
- Federal learning: dividing non IID samples according to Dirichlet distribution
- UNET deployment based on deepstream
- Mysql表insert中文变?号的问题解决办法
- Deep understanding of lambda expressions
- 66.qt quick-qml自定义日历组件(支持竖屏和横屏)
- Play with concurrency: draw a thread state transition diagram
- Go function
- Use a mask to restrict the input of the qlineedit control
- 社交媒体搜索引擎优化及其重要性
- unable to execute xxx. SH: operation not permitted
猜你喜欢

Spring recruitment of Internet enterprises: Kwai meituan has expanded the most, and the annual salary of technical posts is up to nearly 400000

UNET deployment based on deepstream

Federal learning: dividing non IID samples according to Dirichlet distribution

Realizing deep learning framework from zero -- Introduction to neural network

The solution to the complexity brought by lambda expression

Www2022 | know your way back: self training method of graph neural network under distribution and migration

First acquaintance with P4 language

Record the bug of unity 2020.3.31f1 once

66.qt quick-qml自定义日历组件(支持竖屏和横屏)

Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
随机推荐
Yolov5 network modification tutorial (modify the backbone to efficientnet, mobilenet3, regnet, etc.)
【c语言】基础篇学习笔记
Li Kou interview question 02.08 Loop detection
[improvement class] st table to solve the interval maximum value problem [2]
Go variables and constants
初识P4语言
Okcc why is cloud call center better than traditional call center?
What methods should service define?
Binary tree problem solving (2)
Federal learning: dividing non IID samples according to Dirichlet distribution
unable to execute xxx. SH: operation not permitted
记录一次Unity 2020.3.31f1的bug
Let正版短信测压开源源码
Unit testing classic three questions: what, why, and how?
Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification
Keil compilation code of CY7C68013A
Pytoch --- use pytoch to realize u-net semantic segmentation
My first experience of shadowless cloud computer
Deep understanding of lambda expressions