Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

My Blog

从此烟雨落金城,一人撑伞两人行。

Gin数据绑定

POST和GET参数绑定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package main

import "github.com/gin-gonic/gin"

type User struct{
Uaername string `form:"username"`
Password string `form:"password"`
}

func login(c *gin.Context){
var user User
c.ShouldBind(&user) // form表单绑定结构体
c.String(200, "User:%s", user)
}

func main() {
e := gin.Default()
// localhost:8080/login?username=taweizhong&password=111 可以绑定查询参数
e.POST("/login", login)
e.Run()
}

路径绑定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package main
import (
"github.com/gin-gonic/gin"
)

type User struct{
Uaername string `uri:"username"`
Password string `uri:"password"`
}
func login(c *gin.Context){
var user User
c.ShouldBindUri(&user)
c.String(200, "User:%s", user)
}

func main(){
e := gin.Default()
// localhost:8080/login/taweizhong/111
e.GET("/login/:username/:password", login)
e.Run()
}

评论