gin框架使用【4.请求参数】
2024-10-19 15:40:19
GET
url: http://127.0.0.1:8080/users?id=1&name=卷毛狒狒
package main
import (
"github.com/gin-gonic/gin"
"strconv"
)
func main() {
router := gin.Default()
router.GET("/users", func(c *gin.Context) {
idStr := c.Query("id")
id, _ := strconv.Atoi(idStr)
name := c.Query("name")
ageStr := c.DefaultQuery("age", "10")
address, ok := c.GetQuery("address")
if !ok {
address = "gohome"
}
age, _ := strconv.Atoi(ageStr)
c.JSON(200, gin.H{
"id" : id,
"name" : name,
"age" : age,
"address" : address,
})
})
router.Run(":8080")
}
//{"address":"gohome","age":10,"id":1,"name":"juanmaofeifei"}
url: http://127.0.0.1:8080/users?id=1&id=2&id=3
package main import (
"github.com/gin-gonic/gin"
) func main() {
router := gin.Default()
router.GET("/users", func(c *gin.Context) {
idSli := c.QueryArray("id")
c.JSON(200, gin.H{
"ids" : idSli,
})
})
router.Run(":8080")
} //{"ids":["1","2","3"]}
url: http://127.0.0.1:8080/users?user[id]=1&user[name]=卷毛狒狒&user[age]=10
package main import (
"github.com/gin-gonic/gin"
) func main() {
router := gin.Default()
router.GET("/users", func(c *gin.Context) {
user := c.QueryMap("user")
c.JSON(200, gin.H{
"user" : user,
})
})
router.Run(":8080")
} //{"user":{"age":"10","id":"1","name":"卷毛狒狒"}}
最新文章
- OpenCascade Primitives BRep-Cone
- DECLARE_GLOBAL_DATA_PTR宏定义问题
- [c++] Basic ideas and Style Guide
- linux 系统下开机自动启动oracle 监听和实例 (亲测有效)
- MySQL的分页
- PHP Array 函数
- 第二百九十三天 how can I 坚持
- maven和libgdx
- 【Excel】Excel筛选迟点时间的公式
- 用简单的C语言实现多任务轮流切换(模拟操作系统线程机制)
- centos系统安装rar解压工具unar
- [python]socket.listen(backlog)中的backlog含义
- mysql中关于关联索引的问题——对a,b,c三个字段建立联合索引,那么查询时使用其中的2个作为查询条件,是否还会走索引?
- [转]window7下利用DockerToolbox安装Docker
- SAP PA认证
- The Mathematics of the Rubik’s Cube
- python学习day9 字符编码和文件处理
- 微信小程序与手机APP区别
- C++栈学习——顺序栈和链栈的差别
- ACM STEPS——Chapter Two——Section One
热门文章
- 发现Compound的第二个严重漏洞
- VMware安装win7后,安装VMware Tools时报错安装程序无法继续。本程序需要您将此虚拟机上安装的操作系统更新到SP1
- 53端口反弹shell
- djinn
- 【Python 第0课】Why Python?
- Ajax是什么?包含什么技术?有什么作用?
- vue Cannot read property ‘tapPromise‘ of undefined
- rbac-基于角色的权限控制系统(8种常用场景再现)
- 4.3 ROS工作空间覆盖
- linux安装mongodb磁盘空间不足