gin-response

module
v0.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2022 License: MIT

README

gin-response

安装教程
go get -u gitee.com/xinlong-wang/gin-response
使用说明
package main

import (
	"gitee.com/xinlong-wang/gin-response/errcode"
	"gitee.com/xinlong-wang/gin-response/response"
	"github.com/gin-gonic/gin"
)

func main() {

	router := gin.Default()
	router.GET("/", func(ctx *gin.Context) {
		// xml格式数据响应
		response.WrapContextWithXml(ctx).Success("hello,world")
		// json
		response.DefaultWrapContext(ctx).Success("hello,world")
		// 无数据响应
		response.DefaultWrapContext(ctx).DefaultError(errcode.ParamRequired)
		// 自定义错误信息
		response.DefaultWrapContext(ctx).CustomError(1000, "参数错误")
		p := new(struct {
			Name string `json:"name" binding:"required"`
			Age  int8   `json:"age" binding:"required,gte=18"`
		})
		if err := ctx.ShouldBindJSON(p); err != nil {
			response.DefaultWrapContext(ctx).ParamsError(err.(validator.ValidationErrors))
		}
	})

	router.Run(":8088")
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL