当前位置:网站首页>【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格式
菜鸟实战,持续学习!
边栏推荐
- Introduction to the basic concept of queue and typical application examples
- AMQ6126问题解决思路
- Function ‘ngram‘ is not defined
- Minecraft module service opening
- Linux安装Oracle Database 19c
- Sqli labs level 8 (Boolean blind note)
- 2022/2/14 summary
- Nacos download, start and configure MySQL database
- Npoi export word font size correspondence
- C call system sound beep~
猜你喜欢
Redis安装部署(Windows/Linux)
Honeypot attack and defense drill landing application scheme
MYSQL安装出现问题(The service already exists)
Minecraft插件服开服
Solution of Xiaomi TV's inability to access computer shared files
Minecraft air Island service
Sqli labs (post type injection)
Gateway is easy to use
Zipkin is easy to use
Minecraft群组服开服
随机推荐
PCL calculates the intersection of three mutually nonparallel planes
图像变换,转置
Sqli labs level 1
Installing Oracle database 19C for Linux
C# 百度地图,高德地图,Google地图(GPS) 经纬度转换
Minecraft plug-in service opening
2022/2/14 summary
Openfeign is easy to use
CSDN Q & A_ Evaluation
Judge whether it is Sudoku
QT drag event
Minecraft group service opening
Minecraft安装资源包
Essay: RGB image color separation (with code)
Web技术发展史
[flask] ORM one-to-one relationship
Viewing JS array through V8
win10使用docker拉取redis镜像报错read-only file system: unknown
Mirror protocol of synthetic asset track
Shortcut key to comment code and cancel code in idea