1:URL参数提取示例
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
r := gin.New()
r.GET("/ping", func(c *gin.Context) {
///第二个参数是默认数值
name := c.DefaultQuery("name", "xiaozhong")
c.JSON(200, gin.H{
"name": name,
})
})
//不填写的端口的情况下默认的是再8080启动服务
r.Run() // listen and serve on 0.0.0.0:8080
}
image.png
2:Form表单参数的提取
import (
"fmt"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.New()
r.POST("/form", func(c *gin.Context) {
///第二个参数是默认数值
//表单参数设置默认值
type1 := c.DefaultPostForm("type", "alert")
//接收username,password
username := c.PostForm("username")
password := c.PostForm("password")
//接收hobby
hobbys := c.PostFormArray("hobby")
c.String(200,
fmt.Sprintf("type is %s,username is %s,password is %s,hobby is %v", type1, username, password, hobbys))
})
//不填写的端口的情况下默认的是再8080启动服务
r.Run() // listen and serve on 0.0.0.0:8080
}
image.png
3:类似Python蓝图分组--路由分组
package main
import (
"fmt"
"github.com/gin-gonic/gin"
)
func v1get(c *gin.Context) {
name := c.DefaultQuery("name", "xiaozhong")
c.String(200, fmt.Sprintf("V1 %s\n", name))
}
func v2get(c *gin.Context) {
name := c.DefaultQuery("name", "xiaozhong")
c.String(200, fmt.Sprintf("V2 %s\n", name))
}
func main() {
r := gin.New()
//路由组1,处理get请求
v1 := r.Group("/v1")
{
v1.GET("/v1get", v1get)
}
//路由组2,处理post请求
v2 := r.Group("/v2")
{
v2.GET("/v2get", v2get)
}
//不填写的端口的情况下默认的是再8080启动服务
r.Run() // listen and serve on 0.0.0.0:8080
}
image.png
image.png
image.png
4:自定义404的返回
package main
import (
"fmt"
"github.com/gin-gonic/gin"
)
func v1get(c *gin.Context) {
name := c.DefaultQuery("name", "xiaozhong")
c.String(200, fmt.Sprintf("V1 %s\n", name))
}
func currNoRoute(c *gin.Context) {
c.String(200, "没有这个路由地址啊")
}
func main() {
r := gin.New()
r.NoRoute(currNoRoute)
v1 := r.Group("/v1")
{
v1.GET("/v1get", v1get)
}
//不填写的端口的情况下默认的是再8080启动服务
r.Run() // listen and serve on 0.0.0.0:8080
}
image.png
5:POST提交的JSON数据的解析(application/json)
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
//定义接收数据对应的结构体
type Login struct {
//binding装饰到字段为必选字段,若接收为空值,则报错
User string `form:"username" json:"user" uri:"user" xml:"user" binding:"required"`
Password string `form:"password" json:"password" uri:"password" xml:"password" binding:"required"`
}
func main() {
r := gin.New()
//json绑定
r.POST("/json", func(c *gin.Context) {
//声明接收的变量
var json Login
//将request的body中数据,自动按照json格式解析到结构体
if err := c.ShouldBindJSON(&json); err != nil {
//返回错误信息
//gin.H封装了生成json数据工具
c.JSON(http.StatusBadRequest, gin.H{"msg": "失败", "error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": "200", "msg": "成功"})
})
//不填写的端口的情况下默认的是再8080启动服务
r.Run() // listen and serve on 0.0.0.0:8080
}
提示失败的示例:
image.png提示成功的示例:
image.png更多的绑定解析原理一样:
image.png
6:panic异常捕获
package main
import (
"github.com/gin-gonic/gin"
)
func SetUp() gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
c.JSON(500, gin.H{
"message": "系统异常",
})
}
}()
c.Next()
}
}
func main() {
r := gin.New()
r.Use(SetUp())
r.GET("/ping", func(c *gin.Context) {
//panic("系統异常")
c.JSON(200, gin.H{
"message": "pong",
})
})
//不填写的端口的情况下默认的是再8080启动服务
r.Run() // listen and serve on 0.0.0.0:8080
}
网友评论