当前位置:网站首页>[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 !
边栏推荐
- [staff] time sign and note duration (full note | half note | quarter note | eighth note | sixteenth note | thirty second note)
- Matplotlib swordsman Tour - an artist tutorial to accommodate all rivers
- 盘点典型错误之TypeError: X() got multiple values for argument ‘Y‘
- Tensorflow2 keras classification model
- Win10 uses docker to pull the redis image and reports an error read only file system: unknown
- Matplotlib剑客行——没有工具用代码也能画图的造型师
- Use of libusb
- [go practical basis] how can gin get the request parameters of get and post
- Count the number of various characters in the string
- Don't spend money, spend an hour to build your own blog website
猜你喜欢

oracle修改数据库字符集

Minecraft air Island service

Kubernetes deploys Loki logging system

Redis zadd导致的一次线上问题排查和处理

commands out of sync. did you run multiple statements at once

Taking the upgrade of ByteDance internal data catalog architecture as an example, talk about the performance optimization of business system

机器学习之数据类型案例——基于朴素贝叶斯法,用数据辩男女

以字节跳动内部 Data Catalog 架构升级为例聊业务系统的性能优化
![[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)](/img/7f/2cd789339237b7a881bfed7b7545a9.jpg)
[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)

【Go实战基础】gin 如何绑定与使用 url 参数
随机推荐
Cartoon rendering - average normal stroke
队列的基本概念介绍以及典型应用示例
Judge whether it is Sudoku
Talk about the secret of high performance of message queue -- zero copy technology
C Baidu map, Gaode map, Google map (GPS) longitude and latitude conversion
C call system sound beep~
Servlet全解:继承关系、生命周期、容器和请求转发与重定向等
Webflux responsive programming
"Interview high frequency question" is 1.5/5 difficult, and the classic "prefix and + dichotomy" application question
Right click menu of QT
Select sort and insert sort
How to realize asynchronous programming in a synchronous way?
Analysis and solution of a classical Joseph problem
Finishing the interview essentials of secsha system!!!
Gocv split color channel
C # save web pages as pictures (using WebBrowser)
"Redis source code series" learning and thinking about source code reading
机器学习实战:《美人鱼》属于爱情片还是动作片?KNN揭晓答案
C# 调用系统声音 嘀~
Oracle 相关统计