当前位置:网站首页>[go practical basis] how to verify request parameters in gin
[go practical basis] how to verify request parameters in gin
2022-07-02 09:08:00 【Novice practice】
Catalog
3、 ... and 、 Rookie actual combat
(1)GET Method , Add routing parameters and callbacks outside the verification range
(2)GET Method , Add the route parameters and callbacks outside the verification range again
(3)GET Method , Add routing parameters and callbacks within the validation range
One 、 brief introduction
Actual combat scene : How to use gin Verify request parameters
Two 、 Knowledge point
gin route
Structure
Structure parameter binding
Parameter validation
http Status code
3、 ... and 、 Rookie actual combat
Make arrangements now !
1、 establish go file
/*
* @Author: Rookie actual combat
* @Description: gin How to verify request parameters
*/
// Knowledge point :
// # gin route
// # Structure
// # Structure parameter binding
// # Parameter validation
// # http Status code
package main
// Import package
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"runtime"
)
// Defining structure
type Person struct {
Name string `form:"name" binding:"required"`
Age int `form:"age" binding:"required,gt=10"`
}
// The main function
func main() {
// Print using built-in functions
println("Hello", " Rookie actual combat ")
println(" Actual combat scene : ", "gin How to verify request parameters ")
// initialization
r := gin.Default()
// GET Method , Add routing parameters and callbacks
r.GET("/bind", testBind)
r.POST("/bind", testBind)
// Use package functions to print
fmt.Printf(" edition : %s \n", runtime.Version())
// Start the framework program , Default 8080 port
r.Run()
}
// Automatically parse data according to the structure definition
func testBind(c *gin.Context) {
var person Person
// Here is according to the request header Of content-type To do it binding operation
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 Verification mode
// 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、 Running results
Hello Rookie actual combat
Actual combat scene : gin How to verify request parameters
[GIN-debug] Listening and serving HTTP on :8080
(1)GET Method , Add routing parameters and callbacks outside the verification range
(2)GET Method , Add the route parameters and callbacks outside the verification range again
(3)GET Method , Add routing parameters and callbacks within the validation range
Rookie actual combat , Continuous learning !
边栏推荐
- 随笔:RGB图像颜色分离(附代码)
- How to realize asynchronous programming in a synchronous way?
- 盘点典型错误之TypeError: X() got multiple values for argument ‘Y‘
- gocv图片读取并展示
- Matplotlib剑客行——布局指南与多图实现(更新)
- [staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)
- 我服了,MySQL表500W行,居然有人不做分区?
- WSL安装、美化、网络代理和远程开发
- First week of JS study
- Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度
猜你喜欢
Cloud computing in my eyes - PAAS (platform as a service)
十年开发经验的程序员告诉你,你还缺少哪些核心竞争力?
Minecraft install resource pack
Taking the upgrade of ByteDance internal data catalog architecture as an example, talk about the performance optimization of business system
C language implementation of mine sweeping game
[go practical basis] how to install and use gin
Minecraft air Island service
[staff] time mark and note duration (staff time mark | full note rest | half note rest | quarter note rest | eighth note rest | sixteenth note rest | thirty second note rest)
C language - Blue Bridge Cup - 7 segment code
聊聊消息队列高性能的秘密——零拷贝技术
随机推荐
Matplotlib剑客行——初相识Matplotlib
Minecraft plug-in service opening
京东高级工程师开发十年,编写出:“亿级流量网站架构核心技术”
"Redis source code series" learning and thinking about source code reading
win10使用docker拉取redis镜像报错read-only file system: unknown
What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
Kubernetes deploys Loki logging system
Talk about the secret of high performance of message queue -- zero copy technology
Pdf document of distributed service architecture: principle + Design + practice, (collect and see again)
Move a string of numbers backward in sequence
Solution and analysis of Hanoi Tower problem
【Go实战基础】gin 如何设置路由
Dix ans d'expérience dans le développement de programmeurs vous disent quelles compétences de base vous manquez encore?
使用IBM MQ远程连接时报错AMQ 4043解决思路
libusb的使用
Linux安装Oracle Database 19c
图像变换,转置
There is a problem with MySQL installation (the service already exists)
Redis zadd导致的一次线上问题排查和处理
Find the node with the smallest value range in the linked list and move it to the front of the linked list