当前位置:网站首页>【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
// 添加路由参数和回调
// 泛绑定
菜鸟实战,持续学习!
边栏推荐
- C4D quick start tutorial - C4d mapping
- Minecraft plug-in service opening
- Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
- Qt的拖动事件
- 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
- Qunhui NAS configuring iSCSI storage
- First week of JS study
- Kubernetes deploys Loki logging system
- Solution of Xiaomi TV's inability to access computer shared files
- Kubesphere virtualization KSV installation experience
猜你喜欢
随机推荐
Using recursive functions to solve the inverse problem of strings
There is a problem with MySQL installation (the service already exists)
Data asset management function
Googlenet network explanation and model building
QT drag event
OpenFeign 簡單使用
Mysql安装时mysqld.exe报`应用程序无法正常启动(0xc000007b)`
群辉 NAS 配置 iSCSI 存储
C language custom type enumeration, Union (clever use of enumeration, calculation of union size)
win10使用docker拉取redis镜像报错read-only file system: unknown
gocv拆分颜色通道
Linux安装Oracle Database 19c
Tcp/ip - transport layer
Count the number of various characters in the string
Detailed explanation of NIN network
Solution of Xiaomi TV's inability to access computer shared files
Zipkin is easy to use
Synchronize files using unison
Move a string of numbers backward in sequence
How to realize asynchronous programming in a synchronous way?