import ( "github.com/gin-gonic/gin" "net/http" "github.com/sirupsen/logrus")// 1、最简单ginfunc GinSample(){ engine := gin.Default() engine.Any("/",func(context *gin.Context){ context.String(http.StatusOK,"hello world") }) engine.Run(":9025")}// 2、gin各种方法func GinFunc(){ engine := gin.Default() // get engine.GET("/get",func(context *gin.Context){ context.JSON(http.StatusOK,gin.H{ "message":"hello world","status":"done"}) }) // put engine.PUT("/put",func(context *gin.Context){ context.String(http.StatusOK,"put ok") }) // post engine.POST("/post",nil) //... engine.Run(":9025")}// 3、gin 解析入参func GinGetParam(){ engine := gin.Default() engine.GET("/get/:name/*action",func(context *gin.Context){ name := context.Param("Guest") action := context.Param("action") context.String(http.StatusOK,"welcome " + name + action) }) engine.Run(":9025")}// 4、gin 路由组func GinGroup(){ engine := gin.Default() v1 := engine.Group("/v1") { v1.GET("/get",nil) // handlers待实现 v1.POST("post",nil) } v2 := engine.Group("/v2") { v2.GET("/get",nil) v2.POST("/post",nil) } engine.Run(":9025")}// 5、中间件func Middle() { router := gin.Default() // 注册一个路由,使用了 middleware1,middleware2 两个中间件 router.GET("/someGet", middleware1, middleware2, handler) // 默认绑定 :8080 router.Run()}func handler(c *gin.Context) { logrus.Println("exec handler")}func middleware1(c *gin.Context){ // do something c.Next()}func middleware2(c *gin.Context){ // do something c.Next()}