当前位置:网站首页>[go practical basis] gin efficient artifact, how to bind parameters to structures
[go practical basis] gin efficient artifact, how to bind parameters to structures
2022-07-02 09:08:00 【Novice practice】
Catalog
3、 ... and 、 Rookie actual combat
(1) GET Method , Add routing parameters and callbacks
(2) POST Method , Add routing parameters and callbacks
One 、 brief introduction
Actual combat scene : How to use gin Efficient artifact , Bind parameters to structures
Two 、 Knowledge point
gin route
Structure
Structure parameter binding
http Status code
3、 ... and 、 Rookie actual combat
Make arrangements now !
1、 establish go file
/*
* @Author: Rookie actual combat
* @Description: gin Efficient artifact , How to bind parameters to structures
*/
// Knowledge point :
// # gin route
// # Structure
// # Structure parameter binding
// # 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"`
Age string `form:"age"`
}
// The main function
func main() {
// Print using built-in functions
println("Hello", " Rookie actual combat ")
println(" Actual combat scene : ", "gin Efficient artifact , How to bind parameters to structures ")
// 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.StatusOK, "%v \n", person)
} else {
c.String(http.StatusOK, "person bind error\n", err)
}
}
// curl Verification mode
// bind get
// curl -X GET "http://127.0.0.1:8080/bind?name=kitty"
// curl -X GET "http://127.0.0.1:8080/bind?name=kitty&age=21"
// bind post
// curl -X POST "http://127.0.0.1:8080/bind" -d 'name=bob'
// curl -X POST "http://127.0.0.1:8080/bind" -d 'name=bob&age=17'
// bind post json
// curl -H "Content-Type:application/json" -X POST "http://127.0.0.1:8080/bind" -d '{"name":" Hello , Rookie actual combat "}'
// curl -H "Content-Type:application/json" -X POST "http://127.0.0.1:8080/bind" -d '{"name":" Hello , Rookie actual combat ", "age":"18"}'
2、 Running results
Hello Rookie actual combat
Actual combat scene : gin Efficient artifact , How to bind parameters to structures
[GIN-debug] Listening and serving HTTP on :8080
(1) GET Method , Add routing parameters and callbacks
(2) POST Method , Add routing parameters and callbacks
(3) POST Method ,json Format
Rookie actual combat , Continuous learning !
边栏推荐
猜你喜欢
概率还不会的快看过来《统计学习方法》——第四章、朴素贝叶斯法
Web技术发展史
cmd窗口中中文呈现乱码解决方法
Minecraft module service opening
Installing Oracle database 19C for Linux
盘点典型错误之TypeError: X() got multiple values for argument ‘Y‘
聊聊消息队列高性能的秘密——零拷贝技术
Troubleshooting and handling of an online problem caused by redis zadd
【Go实战基础】gin 如何自定义和使用一个中间件
C4D quick start tutorial - C4d mapping
随机推荐
[go practical basis] how to bind and use URL parameters in gin
Linux安装Oracle Database 19c RAC
MYSQL安装出现问题(The service already exists)
队列管理器running状态下无法查看通道
随笔:RGB图像颜色分离(附代码)
Qt——如何在QWidget中设置阴影效果
1、 QT's core class QObject
「面试高频题」难度大 1.5/5,经典「前缀和 + 二分」运用题
十年开发经验的程序员告诉你,你还缺少哪些核心竞争力?
gocv opencv exit status 3221225785
What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
Aneng logistics' share price hit a new low: the market value evaporated by nearly 10 billion yuan, and it's useless for chairman Wang Yongjun to increase his holdings
【Go实战基础】gin 如何绑定与使用 url 参数
Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度
Qt的connect函数和disconnect函数
「Redis源码系列」关于源码阅读的学习与思考
C4D quick start tutorial - Chamfer
C call system sound beep~
First week of JS study
Function ‘ngram‘ is not defined