当前位置:网站首页>【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
// 添加路由参数和回调
// 泛绑定
菜鸟实战,持续学习!
边栏推荐
- Qt的右键菜单
- 群辉 NAS 配置 iSCSI 存储
- Minecraft群組服開服
- C call system sound beep~
- Right click menu of QT
- kubeadm部署kubernetes v1.23.5集群
- Nacos download, start and configure MySQL database
- Gateway is easy to use
- 一、Qt的核心类QObject
- Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
猜你喜欢

kubeadm部署kubernetes v1.23.5集群

Solution of Xiaomi TV's inability to access computer shared files

Function ‘ngram‘ is not defined

Luogu greedy part of the backpack line segment covers the queue to receive water

Driving test Baodian and its spokesperson Huang Bo appeared together to call for safe and civilized travel

查看was发布的应用程序的端口

Minecraft module service opening

Kubedm deploys kubernetes v1.23.5 cluster

WSL installation, beautification, network agent and remote development

将一串数字顺序后移
随机推荐
Use of libusb
kubernetes部署loki日志系统
汉诺塔问题的求解与分析
Sentinel 简单使用
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
Call Stack
C nail development: obtain all employee address books and send work notices
[blackmail virus data recovery] suffix Crylock blackmail virus
Installing Oracle database 19C RAC on Linux
Essay: RGB image color separation (with code)
Mysql安装时mysqld.exe报`应用程序无法正常启动(0xc000007b)`
寻找链表中值域最小的节点并移到链表的最前面
OpenShift 部署应用
群辉 NAS 配置 iSCSI 存储
There is a problem with MySQL installation (the service already exists)
Count the number of various characters in the string
Flex layout
Zipkin is easy to use
Shengshihaotong and Guoao (Shenzhen) new energy Co., Ltd. build the charging pile industry chain
Minecraft群組服開服

