分享
go gin 自动参数绑定工具,rpc支持
xie1xiao1jun · · 3099 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
ginrpc 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)
...... 等接口模式
示例代码
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/xxjwxc/ginrpc/base"
"github.com/xxjwxc/ginrpc/base/api"
)
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) {
fmt.Println(c.Params)
c.String(200, "ok")
}
//TestFun2 自定义context的函数回调地址
func TestFun2(c *api.Context) {
fmt.Println(c.Params)
c.JSON(http.StatusOK, "ok")
}
//TestFun3 带自定义context跟已解析的req参数回调方式
func TestFun3(c *api.Context, req *ReqTest) {
fmt.Println(c.Params)
fmt.Println(req)
c.JSON(http.StatusOK, "ok")
}
//TestFun3 带自定义context跟已解析的req参数回调方式
func TestFun4(c *gin.Context, req ReqTest) {
fmt.Println(c.Params)
fmt.Println(req)
c.JSON(http.StatusOK, 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'
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信3099 次点击
被以下专栏收入,发现更多相似内容
上一篇:Go语言实现HMACSHA1加密
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
ginrpc 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)
...... 等接口模式
示例代码
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/xxjwxc/ginrpc/base"
"github.com/xxjwxc/ginrpc/base/api"
)
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) {
fmt.Println(c.Params)
c.String(200, "ok")
}
//TestFun2 自定义context的函数回调地址
func TestFun2(c *api.Context) {
fmt.Println(c.Params)
c.JSON(http.StatusOK, "ok")
}
//TestFun3 带自定义context跟已解析的req参数回调方式
func TestFun3(c *api.Context, req *ReqTest) {
fmt.Println(c.Params)
fmt.Println(req)
c.JSON(http.StatusOK, "ok")
}
//TestFun3 带自定义context跟已解析的req参数回调方式
func TestFun4(c *gin.Context, req ReqTest) {
fmt.Println(c.Params)
fmt.Println(req)
c.JSON(http.StatusOK, 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'