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

      

  • 相关阅读:
    Finding Action Tubes
    Modeling Video Evolution For Action Recognition
    GBrank_问题列表
    annotation code for human pose estimation
    什么是 kNN 算法?
    什么是强化学习?
    什么是张量?
    什么是遗传/进化算法?
    什么是贝叶斯网络?
    什么是机器学习?
  • 原文地址:https://www.cnblogs.com/tomhuang/p/11579523.html
Copyright © 2011-2022 走看看