12345678910111213141516171819202122232425262728 |
- package handlers
- import (
- "github.com/gin-gonic/gin"
- "net/http"
- )
- func ResponseSuccess(c *gin.Context, data interface{}) {
- c.JSON(http.StatusOK, map[string]interface{}{
- "code": 0,
- "data": data,
- })
- }
- //使用这个方法需要返回自己定义的错误
- func ResponseErr(c *gin.Context, err error) {
- c.JSON(http.StatusOK, err)
- }
- func ResponseErrWithCode(c *gin.Context, code int, err error) {
- var resp = map[string]interface{}{
- "code": code,
- }
- if err != nil {
- resp["err"] = err.Error()
- }
- c.JSON(http.StatusOK, resp)
- }
|