当前位置:网站首页>[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 !
边栏推荐
- Taking the upgrade of ByteDance internal data catalog architecture as an example, talk about the performance optimization of business system
- 盘点典型错误之TypeError: X() got multiple values for argument ‘Y‘
- 以字节跳动内部 Data Catalog 架构升级为例聊业务系统的性能优化
- Talk about the secret of high performance of message queue -- zero copy technology
- Data type case of machine learning -- using data to distinguish men and women based on Naive Bayesian method
- What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
- Introduction to the basic concept of queue and typical application examples
- Avoid breaking changes caused by modifying constructor input parameters
- Redis zadd导致的一次线上问题排查和处理
- 十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?
猜你喜欢
What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
C4D quick start tutorial - Chamfer
Web技术发展史
kubernetes部署loki日志系统
[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)
cmd窗口中中文呈现乱码解决方法
Talk about the secret of high performance of message queue -- zero copy technology
Don't spend money, spend an hour to build your own blog website
Linux binary installation Oracle database 19C
Taking the upgrade of ByteDance internal data catalog architecture as an example, talk about the performance optimization of business system
随机推荐
【Go实战基础】gin 如何自定义和使用一个中间件
Sentinel reports failed to fetch metric connection timeout and connection rejection
Cloudrev self built cloud disk practice, I said that no one can limit my capacity and speed
「Redis源码系列」关于源码阅读的学习与思考
汉诺塔问题的求解与分析
C # save web pages as pictures (using WebBrowser)
Redis sorted set data type API and application scenario analysis
1、 QT's core class QObject
Oracle related statistics
Loadbalancer dynamically refreshes Nacos server
Linux binary installation Oracle database 19C
Pdf document of distributed service architecture: principle + Design + practice, (collect and see again)
判断是否是数独
Cloud computing in my eyes - PAAS (platform as a service)
Don't spend money, spend an hour to build your own blog website
Complete solution of servlet: inheritance relationship, life cycle, container, request forwarding and redirection, etc
C language implementation of mine sweeping game
Minecraft air Island service
Judge whether it is Sudoku
使用IBM MQ远程连接时报错AMQ 4043解决思路