gin自动参数绑定工具

栏目: Go · 发布时间: 5年前

内容简介:1、 目录结构说明2、api接口说明

golang gin 参数自动绑定工具

  • 基于 go-gin 的 json restful 风格的golang基础库
  • 自带请求参数过滤及绑定实现
  • 代码注册简单且支持多种注册方式

1、 目录结构说明

  • ginrpc/base/common.go 基础库
  • ginrpc/base/api/context.go 自定义context内容
  • 支持参数自动检测 binding:"required"validator
  • 支持rpc自动映射

2、api接口说明

支持3种接口模式

  • func(*gin.Context) //gogin 原始接口
  • func(*api.Context) //自定义的context类型
  • func(*api.Context,req) //自定义的context类型,带request 请求参数 func(*gin.Context,*req) ...... 等接口模式

示例代码

type ReqTest struct {
     Access_token string `json:"access_token"`                 //access_token
     UserName     string `json:"user_name" binding:"required"` //用户名
     Password     string `json:"password"`                     //新密码
 }

 //TestFun1 gin 默认的函数回调地址
 func TestFun1(c *gin.Context) {
 }

 //TestFun2 自定义context的函数回调地址
 func TestFun2(c *api.Context) {
 }

 //TestFun3 带自定义context跟已解析的req参数回调方式
 func TestFun3(c *api.Context, req *ReqTest) {
     fmt.Println(req)
 }

 //TestFun3 带自定义context跟已解析的req参数回调方式
 func TestFun4(c *gin.Context, req ReqTest) {
     fmt.Println(req)
 }

 func main() {
     router := gin.Default()
     router.POST("/test1", base.GetHandlerFunc(TestFun1))
     router.POST("/test2", base.GetHandlerFunc(TestFun2))
     router.POST("/test3", base.GetHandlerFunc(TestFun3))
     router.POST("/test4", base.GetHandlerFunc(TestFun4))

     router.Run(":8080")
 }
  • curl
    curl 'http://127.0.0.1:8080/test4' -H 'Content-Type: application/json' -d '{"access_token":"111", "user_name":"222", "password":"333"}'

传送门

如果你喜欢,请'star'


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

闪魂FLASH8网站建设实录

闪魂FLASH8网站建设实录

马谧铤 / 中国林业 / 2006-7 / 46.00元

《闪魂FLASH8网站建设实录》旨在提供以Flash(Flash 8.0为创作工具)为技术核心的整套互动网站的开发思路,其中包括了网站策划、平面设计、程序设计等实用的互联网应用技术。内容包括Photoshop CS2设计,FIash 8创作和ActionScript应用程序开发的操作流程。在技术学习的过程中.大家还将体会到顶级互动网站设计、网站建设的设计流程和思路。《闪魂FLASH8网站建设实录》......一起来看看 《闪魂FLASH8网站建设实录》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器