Gin路由基础
路由的基本使用
gin 框架中采用的路由库是基于httprouter做的
地址为:https://github.com/julienschmidt/httprouter
基本路由
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func funcPost(c *gin.Context) {
c.String(http.StatusOK, "post请求")
}
func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "hello word")
})
r.POST("/",funcPost)
//r.DELETE()
//r.PUT()
//r.OPTIONS()
//监听端口默认为8080
r.Run(":8000")
}
获取路径中参数(动态路由)
可以通过Context的Param方法来获取API参数
例子:
localhost:8000/user/lqz/nb
/:name表示一个字符串或int类型
/*action表示任意字符串,包括/,如 /nb/hadsome,*号类型的参数,表示匹配所有
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
func main() {
r := gin.Default()
r.GET("/user/:name/*action", func(c *gin.Context) {
name := c.Param("name")
action := c.Param("action")
fmt.Println(action)
//把字符串开头/截取掉
action = strings.Trim(action, "/")
fmt.Println(action)
c.String(http.StatusOK, name+" is "+action)
})
//默认为监听8080端口
r.Run(":8000")
}

获取请求地址中参数
URL参数可以通过DefaultQuery()或Query()方法获取
DefaultQuery()若参数不存在,返回默认值,Query()若不存在,返回空串
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/user", func(c *gin.Context) {
//指定默认值
//http://localhost:8080/user 才会打印出来默认的值
//name := c.DefaultQuery("name", "世界")
name := c.Query("name")
c.String(http.StatusOK, fmt.Sprintf("hello %s", name))
})
r.Run()
}
不传递参数输出的结果:

传递参数输出的结果:

获取表单参数
表单传输为post请求,http常见的传输格式为四种:
application/json
application/x-www-form-urlencoded
application/xml
multipart/form-data
表单参数可以通过PostForm()方法获取,该方法默认解析的是x-www-form-urlencoded或from-data格式的参数
Json 编码格式解析到结构体
客户端传参,后端接收并解析到结构体定
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
// 定义接收数据的结构体
type Login struct {
// binding:"required"修饰的字段,若接收为空值,则报错,是必须字段
User string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
}
func main() {
// 1.创建路由
r := gin.Default()
// JSON绑定
r.POST("/loginJSON", func(c *gin.Context) {
// 声明接收的变量
var login Login
// 将request的body中的数据,自动按照json格式解析到结构体(只能解析json格式)
if err := c.ShouldBindJSON(&login); err != nil {
// 返回错误信息
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// 判断用户名密码是否正确
if login.User == "lqz" && login.Password == "123" {
c.JSON(http.StatusBadRequest, gin.H{"status": "100", "msg": "登陆成功"})
}else {
c.JSON(http.StatusOK, gin.H{"status": "101","msg": "用户名或密码错误"})
}
})
r.Run(":8000")
}
使用postman测试,编码使用json格式,向http://127.0.0.1:8000/loginJSON发送post请求
{"username":"lqz","password":"123"}
post或get提交数据解析到结构体
ShouldBind会按照下面的顺序解析请求中的数据完成绑定:
如果是 GET 请求,http://127.0.0.1:8000/loginForm/?username=lqz&password=123
如果是 POST 请求,三种编码格式都支持
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
// 定义接收数据的结构体
type Login struct {
// binding:"required"修饰的字段,若接收为空值,则报错,是必须字段
User string `form:"username" json:"username" uri:"username" binding:"required"`
Password string `form:"password" json:"password" uri:"password" binding:"required"`
}
func main() {
router := gin.Default()
// 绑定JSON的示例 ({"username": "lqz", "password": "123"})
router.POST("/loginJSON", func(c *gin.Context) {
var login Login
if err := c.ShouldBind(&login); err == nil {
c.JSON(http.StatusOK, gin.H{
"user": login.User,
"password": login.Password,
})
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
})
// 绑定QueryString示例 (http://127.0.0.1:8000/loginForm/?username=lqz&password=123)
router.GET("/loginForm", func(c *gin.Context) {
var login Login
// ShouldBind()会根据请求的Content-Type自行选择绑定器
if err := c.ShouldBind(&login); err == nil {
c.JSON(http.StatusOK, gin.H{
"user": login.User,
"password": login.Password,
})
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
})
router.Run(":8000")
}

浙公网安备 33010602011771号