当前位置:网站首页>【Go实战基础】gin 如何绑定与使用 url 参数
【Go实战基础】gin 如何绑定与使用 url 参数
2022-07-02 06:33:00 【菜鸟实战】
目录
一、简介
本篇用来介绍 gin 如何绑定与使用 url 参数
二、知识点
- gin 路由
- 参数绑定与映射
- 获取 url 参数
- 根据参数,返回获取的参数数据
- 泛绑定
三、菜鸟实战
实战场景:用 gin 如何绑定与使用 url 参数
马上安排!
1、创建 go文件
package main
// 导入包
import (
"fmt"
"runtime"
"github.com/gin-gonic/gin"
)
// 主函数
func main() {
// 使用内置函数打印
println("Hello", "菜鸟实战")
println("实战场景: ", "gin 如何绑定与使用 url 参数")
// 初始化
r := gin.Default()
// 添加路由参数和回调
r.GET("/:name/:id", func(c *gin.Context) {
// 返回的 code 和 对应的结构体信息
c.JSON(200, gin.H{
"name": c.Param("name"),
"id": c.Param("id"),
})
})
// 泛绑定
r.GET("/user/*action", func(c *gin.Context) {
// 返回的 code 和 对应的结构体信息
c.String(200, "命中泛绑定路由规则 \n")
})
// 使用包函数打印
fmt.Printf("版本: %s \n", runtime.Version())
// 启动框架程序, 默认 8080 端口
r.Run()
}
// curl 验证方式
// curl -X GET "http://127.0.0.1:8080/tom/5"
// curl -X GET "http://127.0.0.1:8080/user/addaction"
// curl -X GET "http://127.0.0.1:8080/user/updateaction"
2、运行结果
Hello 菜鸟实战
实战场景: gin 如何绑定与使用 url 参数
[GIN-debug] Listening and serving HTTP on :8080
// 添加路由参数和回调
// 泛绑定
菜鸟实战,持续学习!
边栏推荐
- Leetcode sword finger offer brush questions - day 22
- Linux二进制安装Oracle Database 19c
- Webflux responsive programming
- Web技术发展史
- Oracle related statistics
- gocv图片裁剪并展示
- Minecraft module service opening
- Linux安装Oracle Database 19c
- How to realize asynchronous programming in a synchronous way?
- Getting started with k8s: building MySQL with Helm
猜你喜欢
OpenShift 部署应用
Openshift deployment application
Driving test Baodian and its spokesperson Huang Bo appeared together to call for safe and civilized travel
During MySQL installation, mysqld Exe reports that the application cannot start normally (0xc000007b)`
Tcp/ip - transport layer
[blackmail virus data recovery] suffix Hydra blackmail virus
Minecraft模组服开服
OpenShift构建镜像
kubernetes部署loki日志系统
Linux安装Oracle Database 19c
随机推荐
cmd窗口中中文呈现乱码解决方法
kubeadm部署kubernetes v1.23.5集群
MYSQL安装出现问题(The service already exists)
Qt——如何在QWidget中设置阴影效果
Leetcode sword finger offer brush questions - day 23
How to realize asynchronous programming in a synchronous way?
Introduction to the basic concept of queue and typical application examples
Data asset management function
汉诺塔问题的求解与分析
PCL calculates the intersection of three mutually nonparallel planes
Leetcode sword finger offer brush questions - day 22
Essay: RGB image color separation (with code)
Win10 uses docker to pull the redis image and reports an error read only file system: unknown
Connect function and disconnect function of QT
Programmer training, crazy job hunting, overtime ridiculed by colleagues deserve it
2022/2/13 summary
Hengyuan cloud_ Can aiphacode replace programmers?
C language - Blue Bridge Cup - 7 segment code
gocv图片读取并展示
Linux安装Oracle Database 19c