当前位置:网站首页>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 :
边栏推荐
- Wechat applet calculates the distance between the two places
- Federal learning: dividing non IID samples according to Dirichlet distribution
- Yyds dry goods inventory kubernetes introduction foundation pod concept and related operations
- Free drawing software recommended - draw io
- win11安装pytorch-gpu遇到的坑
- 60后关机程序
- Pytorch---使用Pytorch进行图像定位
- One click generation and conversion of markdown directory to word format
- Dare to go out for an interview without learning some distributed technology?
- [source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
猜你喜欢

Actual combat | use composite material 3 in application

CorelDRAW Graphics Suite2022免费图形设计软件

Pytorch---使用Pytorch进行鸟类的预测

10 minutes to understand CMS garbage collector in JVM

Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification

BGP experiment the next day

Installation and use of blue lake

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

The solution to the complexity brought by lambda expression

Exposure X8标准版图片后期滤镜PS、LR等软件的插件
随机推荐
There is no prompt for SQL in idea XML, and the dialect setting is useless.
万卷共知,一书一页总关情,TVP读书会带你突围阅读迷障!
CorelDRAW Graphics Suite2022免费图形设计软件
LxC limits the number of CPUs
Read "the way to clean code" - function names should express their behavior
Geotrust OV Multi - Domain Domain SSL Certificate rmb2100 per year contains several Domain names?
Flag bits in assembly language: CF, PF, AF, ZF, SF, TF, if, DF, of
FAQ | FAQ for building applications for large screen devices
[C language] basic learning notes
C language guessing numbers game
Ognl和EL表达式以及内存马的安全研究
Thinkphp內核工單系統源碼商業開源版 多用戶+多客服+短信+郵件通知
Realizing deep learning framework from zero -- Introduction to neural network
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
Binary tree problem solving (1)
Unit testing classic three questions: what, why, and how?
Pytoch --- use pytoch to predict birds
Shenzhen will speed up the cultivation of ecology to build a global "Hongmeng Oula city", with a maximum subsidy of 10million yuan for excellent projects
Www2022 | know your way back: self training method of graph neural network under distribution and migration
Play with concurrency: what's the use of interruptedexception?