• gin框架


    func main() {
    	r := gin.Default()
    	r.GET("/ping", func(c *gin.Context) {
    		c.JSON(200, gin.H{
    			"message": "pong",
    		})
    	})
    	r.Run()
    }
    

      

    gin.Default()

    返回一个带日志记录器和其它中间件的引擎

    // Default returns an Engine instance with the Logger and Recovery middleware already attached.
    func Default() *Engine {
    	debugPrintWARNINGDefault()
    	engine := New()
    	engine.Use(Logger(), Recovery())
    	return engine
    }
    

      

    r.GET

    get方法路由的简写

    // GET is a shortcut for router.Handle("GET", path, handle).
    func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) IRoutes {
    	return group.handle("GET", relativePath, handlers)
    }
    

      

    c.JSON

    将指定的结构体序列化成JSON,并给到response,同时设置格式为json

    // JSON serializes the given struct as JSON into the response body.
    // It also sets the Content-Type as "application/json".
    func (c *Context) JSON(code int, obj interface{}) {
    	c.Render(code, render.JSON{Data: obj})
    }
    

     

    gin.H

     对map的简写

    // H is a shortcut for map[string]interface{}
    type H map[string]interface{}
    

      

    c.run()

    启动服务,并监听指定地址和端口

    // Run attaches the router to a http.Server and starts listening and serving HTTP requests.
    // It is a shortcut for http.ListenAndServe(addr, router)
    // Note: this method will block the calling goroutine indefinitely unless an error happens.
    func (engine *Engine) Run(addr ...string) (err error) {
    	defer func() { debugPrintError(err) }()
    
    	address := resolveAddress(addr)
    	debugPrint("Listening and serving HTTP on %s
    ", address)
    	err = http.ListenAndServe(address, engine)
    	return
    }
    

      

  • 相关阅读:
    python---逻辑运算(and、or、not)
    VsCode预览HTML文件的方法
    Windows10下composer的安装以及配置
    Centos7上SVN客户端的安装和使用
    Centos7安装redis
    centos7下解决yum install mysql-server没有可用包
    Centos7 安装的mysql 报错 Unit mysqld.service entered failed state.
    Redis的一些规范
    PHP安装Redis扩展
    bzip2:无法 exec: 没有那个文件或目录
  • 原文地址:https://www.cnblogs.com/tomhuang/p/11579523.html
Copyright © 2020-2023  润新知