当前位置:网站首页>【Go实战基础】gin 如何验证请求参数
【Go实战基础】gin 如何验证请求参数
2022-07-02 06:33:00 【菜鸟实战】
目录
一、简介
实战场景: 如何使用 gin 验证请求参数
二、知识点
gin 路由
结构体
结构体参数绑定
参数验证
http 状态码
三、菜鸟实战
马上安排!
1、创建 go文件
/*
* @Author: 菜鸟实战
* @Description: gin 如何验证请求参数
*/
// 知识点:
// # gin 路由
// # 结构体
// # 结构体参数绑定
// # 参数验证
// # http 状态码
package main
// 导入包
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"runtime"
)
// 定义结构体
type Person struct {
Name string `form:"name" binding:"required"`
Age int `form:"age" binding:"required,gt=10"`
}
// 主函数
func main() {
// 使用内置函数打印
println("Hello", "菜鸟实战")
println("实战场景: ", "gin 如何验证请求参数")
// 初始化
r := gin.Default()
// GET 方法, 添加路由参数和回调
r.GET("/bind", testBind)
r.POST("/bind", testBind)
// 使用包函数打印
fmt.Printf("版本: %s \n", runtime.Version())
// 启动框架程序, 默认 8080 端口
r.Run()
}
// 自动根据结构体定义解析数据
func testBind(c *gin.Context) {
var person Person
// 这里是根据请求 header 的 content-type 来做 binding 操作
if err := c.ShouldBind(&person); err != nil {
c.String(http.StatusInternalServerError, "person bind error %v \n", err)
} else {
c.String(http.StatusOK, "%v \n", person)
}
}
// curl 验证方式
// bind get
// curl -X GET "http://127.0.0.1:8080/bind?a=1"
// curl -X GET "http://127.0.0.1:8080/bind?name=kitty&age=5"
// curl -X GET "http://127.0.0.1:8080/bind?name=kitty&age=21"
2、运行结果
Hello 菜鸟实战
实战场景: gin 如何验证请求参数
[GIN-debug] Listening and serving HTTP on :8080
(1)GET 方法, 添加验证范围外的路由参数和回调

(2)GET 方法, 再次添加验证范围外的路由参数和回调

(3)GET 方法, 添加验证范围内的路由参数和回调

菜鸟实战,持续学习!
边栏推荐
- Sqli labs level 1
- Right click menu of QT
- Detailed explanation of NIN network
- QT drag event
- Minecraft模组服开服
- Connect function and disconnect function of QT
- C language custom types - structure, bit segment (anonymous structure, self reference of structure, memory alignment of structure)
- Select sort and insert sort
- 1、 QT's core class QObject
- Oracle related statistics
猜你喜欢

Linux安装Oracle Database 19c RAC

Sqli labs level 12

Shengshihaotong and Guoao (Shenzhen) new energy Co., Ltd. build the charging pile industry chain

Driving test Baodian and its spokesperson Huang Bo appeared together to call for safe and civilized travel

Service de groupe minecraft

Linux binary installation Oracle database 19C

Solution and analysis of Hanoi Tower problem

Synchronize files using unison

队列的基本概念介绍以及典型应用示例

小米电视不能访问电脑共享文件的解决方案
随机推荐
Win10 uses docker to pull the redis image and reports an error read only file system: unknown
Kubesphere virtualization KSV installation experience
What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
Service de groupe minecraft
Installing Oracle database 19C RAC on Linux
Luogu greedy part of the backpack line segment covers the queue to receive water
Find the node with the smallest value range in the linked list and move it to the front of the linked list
Nacos 下载启动、配置 MySQL 数据库
C Gaode map obtains the address according to longitude and latitude
Pclpy projection filter -- projection of point cloud to cylinder
commands out of sync. did you run multiple statements at once
Application of kotlin - higher order function
Pyspark de duplication dropduplicates, distinct; withColumn、lit、col; unionByName、groupBy
C # save web pages as pictures (using WebBrowser)
zipkin 简单使用
MYSQL安装出现问题(The service already exists)
Loadbalancer dynamically refreshes Nacos server
Tcp/ip - transport layer
Zipkin is easy to use
OpenFeign 简单使用