Go——gin 框架基本入门

概述

  • 为何不选择 beego,理由很简单,其设计风格已经不符合现在开发潮流,现实往往就是这样,看似完美实则鸡肋

  • gin 最大的特点,也是我最为看重的就是中间件功能,说真的,当我在 gin 框架中看到 next 这个关键字,心里真的有点小激动

中间件

  • 全局中间件

    import (
    	"log"
    	"net/http"
    	"github.com/gin-gonic/gin"
    )
    
    func logger() gin.HandlerFunc {
    
    	return func(c *gin.Context) {
    		log.Println("before is next")
    		c.Next()
    		log.Println("after is next ")
    	}
    }
    
    func main() {
    
    	r := gin.New()
    
    	r.Use(logger())
    
    	r.GET("/user/:name", func(c *gin.Context) {
    		name := c.Param("name")
    		c.String(http.StatusOK, "Hello %s", name)
    	})
    
    	r.GET("/topic/:id", func(c *gin.Context) {
    		id := c.Param("id")
    		c.String(http.StatusOK, "you search is %s", id)
    	})
    
    	r.Run(":8080")
    }
    
  • 路由中间件

    func m1() gin.HandlerFunc {
    	return func(c *gin.Context) {
    
    		log.Println("before is m1")
    		c.Next()
    		log.Println("after is m1 ")
    	}
    }
    
    func m2() gin.HandlerFunc {
    	return func(c *gin.Context) {
    
    		log.Println("before is m2")
    		c.Next()
    		log.Println("after is m2 ")
    	}
    }
    
    func main() {
    
    	r := gin.New()
    
    	r.GET("/user/:name", m1(), func(c *gin.Context) {
    		name := c.Param("name")
    		c.String(http.StatusOK, "Hello %s", name)
    	})
    
    	r.GET("/topic/:id", m2(), func(c *gin.Context) {
    		id := c.Param("id")
    		c.String(http.StatusOK, "you search is %s", id)
    	})
    
    	r.Run(":8080")
    }
    
  • 路由组中间件

    func main() {
    
    	r := gin.New()
    
    	v1 := r.Group("v1", m1())
    	v1.GET("/user/:name", func(c *gin.Context) {
    		name := c.Param("name")
    		c.String(http.StatusOK, "Hello %s", name)
    	})
    	v1.GET("/topic/:id", func(c *gin.Context) {
    		id := c.Param("id")
    		c.String(http.StatusOK, "you search is %s", id)
    	})
    
    	v2 := r.Group("v2", m2())
    	v2.GET("/user/:name", func(c *gin.Context) {
    		name := c.Param("name")
    		c.String(http.StatusOK, "Hello %s", name)
    	})
    	v2.GET("/topic/:id", func(c *gin.Context) {
    		id := c.Param("id")
    		c.String(http.StatusOK, "you search is %s", id)
    	})
    
    	r.Run(":8080")
    }
    
  • 在中间件或处理程序中启动新的 Goroutines 时,您不应该使用其中的原始上下文,您必须使用只读副本

    func main() {
    
    	r := gin.Default()
    
    	r.GET("/long_async", func(c *gin.Context) {
    		// create copy to be used inside the goroutine
    		cCp := c.Copy()
    		go func() {
    			// simulate a long task with time.Sleep(). 5 seconds
    			time.Sleep(5 * time.Second)
    
    			// note that you are using the copied context "cCp", IMPORTANT
    			log.Println("Done! in path " + cCp.Request.URL.Path)
    		}()
    	})
    
    	r.GET("/long_sync", func(c *gin.Context) {
    		// simulate a long task with time.Sleep(). 5 seconds
    		time.Sleep(5 * time.Second)
    
    		// since we are NOT using a goroutine, we do not have to copy the context
    		log.Println("Done! in path " + c.Request.URL.Path)
    	})
    
    	// Listen and serve on 0.0.0.0:8080
    	r.Run(":8080")
    }
    

帮助文档

posted @ 2018-08-05 23:55  cnloop  阅读(645)  评论(0编辑  收藏  举报