zoukankan      html  css  js  c++  java
  • 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")
      }
      

    帮助文档

  • 相关阅读:
    2017/09/02笔记:ps
    207/08/3学习笔记:pc端网站如何实现移动端适配知识点
    2017/0828xueix笔记:图像替代文本&css绘制的图形
    20170824:面试题笔记
    目前比较全的CSS重设(reset)方法总结
    学习笔记:css3实现多行文字溢出显示省略号&display:box;
    SVG圆形<circle> 标签
    k8s节点分配nodeSelector, Affinity和Anti-Affinity 亲和性和反亲和性
    kubernetes网络介绍
    centos6.7 安装docker
  • 原文地址:https://www.cnblogs.com/cnloop/p/9427961.html
Copyright © 2011-2022 走看看