当前位置:网站首页>[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 !
边栏推荐
- libusb的使用
- Synchronize files using unison
- C#钉钉开发:取得所有员工通讯录和发送工作通知
- Minecraft air Island service
- Count the number of various characters in the string
- Kubernetes deploys Loki logging system
- Qt的右键菜单
- Pyspark de duplication dropduplicates, distinct; withColumn、lit、col; unionByName、groupBy
- 概率还不会的快看过来《统计学习方法》——第四章、朴素贝叶斯法
- Servlet全解:继承关系、生命周期、容器和请求转发与重定向等
猜你喜欢

Matplotlib剑客行——布局指南与多图实现(更新)

What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?

Flink-使用流批一体API统计单词数量

小米电视不能访问电脑共享文件的解决方案

Don't spend money, spend an hour to build your own blog website

Linux安装Oracle Database 19c RAC

Data type case of machine learning -- using data to distinguish men and women based on Naive Bayesian method
![[go practical basis] how to bind and use URL parameters in gin](/img/63/84717b0da3a55d7fda9d57c8da2463.png)
[go practical basis] how to bind and use URL parameters in gin
![[staff] time sign and note duration (full note | half note | quarter note | eighth note | sixteenth note | thirty second note)](/img/bf/2b0b9c640bdad2c55293f905a22055.jpg)
[staff] time sign and note duration (full note | half note | quarter note | eighth note | sixteenth note | thirty second note)

Matplotlib剑客行——初相识Matplotlib
随机推荐
Tensorflow2 keras classification model
Minecraft air Island service
随笔:RGB图像颜色分离(附代码)
QT -- how to set shadow effect in QWidget
Driving test Baodian and its spokesperson Huang Bo appeared together to call for safe and civilized travel
Mysql安装时mysqld.exe报`应用程序无法正常启动(0xc000007b)`
[staff] time sign and note duration (full note | half note | quarter note | eighth note | sixteenth note | thirty second note)
C Baidu map, Gaode map, Google map (GPS) longitude and latitude conversion
Troubleshooting and handling of an online problem caused by redis zadd
以字节跳动内部 Data Catalog 架构升级为例聊业务系统的性能优化
[staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)
gocv图片裁剪并展示
MYSQL安装出现问题(The service already exists)
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
Qt——如何在QWidget中设置阴影效果
【Go实战基础】gin 如何绑定与使用 url 参数
C # save web pages as pictures (using WebBrowser)
Cloud computing in my eyes - PAAS (platform as a service)
What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
Openshift container platform community okd 4.10.0 deployment





