当前位置:网站首页>【Go实战基础】gin 高效神器,如何将参数绑定到结构体
【Go实战基础】gin 高效神器,如何将参数绑定到结构体
2022-07-02 06:33:00 【菜鸟实战】
目录
一、简介
实战场景: 如何使用 gin 高效神器,将参数绑定到结构体
二、知识点
gin 路由
结构体
结构体参数绑定
http 状态码
三、菜鸟实战
马上安排!
1、创建 go文件
/*
* @Author: 菜鸟实战
* @Description: gin 高效神器,如何将参数绑定到结构体
*/
// 知识点:
// # gin 路由
// # 结构体
// # 结构体参数绑定
// # http 状态码
package main
// 导入包
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"runtime"
)
// 定义结构体
type Person struct {
Name string `form:"name"`
Age string `form:"age"`
}
// 主函数
func main() {
// 使用内置函数打印
println("Hello", "菜鸟实战")
println("实战场景: ", "gin 高效神器,如何将参数绑定到结构体")
// 初始化
r := gin.Default()
// GET 方法, 添加路由参数和回调
r.GET("/bind", testBind)
r.POST("/bind", testBind)
// 使用包函数打印
fmt.Printf("版本: %s \n", runtime.Version())
// 启动框架程序, 默认 8080 端口
r.Run()
}
// 自动根据结构体定义解析数据
func testBind(c *gin.Context) {
var person Person
// 这里是根据请求 header 的 content-type 来做 binding 操作
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 验证方式
// 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":"你好, 菜鸟实战"}'
// curl -H "Content-Type:application/json" -X POST "http://127.0.0.1:8080/bind" -d '{"name":"你好, 菜鸟实战", "age":"18"}'
2、运行结果
Hello 菜鸟实战
实战场景: gin 高效神器,如何将参数绑定到结构体
[GIN-debug] Listening and serving HTTP on :8080
(1) GET 方法, 添加路由参数和回调
(2) POST 方法, 添加路由参数和回调
(3) POST 方法,json格式
菜鸟实战,持续学习!
边栏推荐
- 查看was发布的应用程序的端口
- HackTheBox-Gunship
- [blackmail virus data recovery] suffix Crylock blackmail virus
- Kubernetes deploys Loki logging system
- MYSQL安装出现问题(The service already exists)
- Openshift deployment application
- Gocv split color channel
- Gocv boundary fill
- Openshift build image
- Kubedm deploys kubernetes v1.23.5 cluster
猜你喜欢
查看was发布的应用程序的端口
Solution and analysis of Hanoi Tower problem
Synchronize files using unison
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
Installing Oracle database 19C RAC on Linux
Googlenet network explanation and model building
Web security -- core defense mechanism
Kubernetes deploys Loki logging system
Linux二进制安装Oracle Database 19c
C4D quick start tutorial - Chamfer
随机推荐
Count the number of various characters in the string
[blackmail virus data recovery] suffix Rook3 blackmail virus
Programmer training, crazy job hunting, overtime ridiculed by colleagues deserve it
Qt的拖动事件
Mysql安装时mysqld.exe报`应用程序无法正常启动(0xc000007b)`
Tensorflow2 keras 分类模型
kubernetes部署loki日志系统
cmd窗口中中文呈现乱码解决方法
Driving test Baodian and its spokesperson Huang Bo appeared together to call for safe and civilized travel
Introduction to the basic concept of queue and typical application examples
Qunhui NAS configuring iSCSI storage
Shengshihaotong and Guoao (Shenzhen) new energy Co., Ltd. build the charging pile industry chain
Redis安装部署(Windows/Linux)
1、 QT's core class QObject
队列的基本概念介绍以及典型应用示例
Pyspark de duplication dropduplicates, distinct; withColumn、lit、col; unionByName、groupBy
Sentinel reports failed to fetch metric connection timeout and connection rejection
Oracle related statistics
Gocv split color channel
C# 高德地图 根据经纬度获取地址