设为首页 加入收藏

TOP

[系列] go-gin-api 规划目录和参数验证(二)(二)
2019-09-03 03:44:35 】 浏览:108
Tags:系列 go-gin-api 规划 目录 参数 验证
e int, msg string, data interface{}) { g.Ctx.JSON(200, response{ Code : code, Message : msg, Data : data, }) return }

控制器调用(controller/product.go):

utilGin := util.Gin{Ctx:c}
if err := c.ShouldBind(&param_bind.ProductAdd{}); err != nil {
    utilGin.Response(-1, err.Error(), nil)
    return
}

咱们用 Postman 模拟 post 请求时,name 参数不传或传递为空,会出现:

{
    "code": -1,
    "msg": "Key: 'ProductAdd.Name' Error:Field validation for 'Name' failed on the 'required' tag",
    "data": null
}

name=admin 时:

{
    "code": -1,
    "msg": "Key: 'ProductAdd.Name' Error:Field validation for 'Name' failed on the 'Nameva lid' tag",
    "data": null
}

OK,上面两个验证都生效了!

源码地址

https://github.com/xinliangnote/go-gin-api

go-gin-api 系列文章

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇windows下的go get 显示进度 下一篇通过 Channel 实现 Goroutine Pool

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目