golang gin框架

2020-07-05  本文已影响0人  hehehehe

感觉 gin比iris好太多了,gin基本还比较符合原生http的模式
iris封装的对新手不友好
post参数分为json(表单)和非json

package main

import(
    "github.com/gin-gonic/gin"
    "file-storage/handler"
)

func main()  {

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

    // router.GET("/someGet", GetHandler)
    router.POST("/post", handler.PostHandler)

    // 默认启动的是 8080端口,也可以自己定义启动端口
    router.Run()
}
package handler

import(
   "github.com/gin-gonic/gin"
)

func PostHandler(c *gin.Context)  {
   chk_id := c.PostForm("chk_id")
   chk_id2 := c.Query("chk_id")
   path := c.DefaultPostForm("path", "anonymous") // 此方法可以设置默认值

   c.JSON(200, gin.H{
       "status": "posted",
       "message": "ok",
       "path": path,
       "chk_id": chk_id,
       "chk_id2": chk_id2,
   })
}


上一篇 下一篇

猜你喜欢

热点阅读