当前位置:网站首页>[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 !
边栏推荐
- How to realize asynchronous programming in a synchronous way?
- 京东高级工程师开发十年,编写出:“亿级流量网站架构核心技术”
- gocv图片读取并展示
- "Redis source code series" learning and thinking about source code reading
- Qt QTimer类
- MYSQL安装出现问题(The service already exists)
- Cartoon rendering - average normal stroke
- [go practical basis] how to install and use gin
- AMQ6126问题解决思路
- 队列管理器running状态下无法查看通道
猜你喜欢
Function ‘ngram‘ is not defined
Synchronize files using unison
WSL installation, beautification, network agent and remote development
Matplotlib swordsman Tour - an artist tutorial to accommodate all rivers
Openshift container platform community okd 4.10.0 deployment
How to realize asynchronous programming in a synchronous way?
Multi version concurrency control mvcc of MySQL
[staff] time sign and note duration (full note | half note | quarter note | eighth note | sixteenth note | thirty second note)
cmd窗口中中文呈现乱码解决方法
Minecraft air Island service
随机推荐
Complete solution of servlet: inheritance relationship, life cycle, container, request forwarding and redirection, etc
Gocv image reading and display
Loadbalancer dynamically refreshes Nacos server
[staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)
commands out of sync. did you run multiple statements at once
Redis安装部署(Windows/Linux)
「面试高频题」难度大 1.5/5,经典「前缀和 + 二分」运用题
Essay: RGB image color separation (with code)
[go practical basis] how to bind and use URL parameters in gin
"Redis source code series" learning and thinking about source code reading
First week of JS study
分布式服务架构精讲pdf文档:原理+设计+实战,(收藏再看)
队列的基本概念介绍以及典型应用示例
Select sort and insert sort
Mirror protocol of synthetic asset track
Matplotlib剑客行——初相识Matplotlib
Matplotlib剑客行——布局指南与多图实现(更新)
Gocv split color channel
libusb的使用
Sentinel reports failed to fetch metric connection timeout and connection rejection