当前位置:网站首页>【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
// 添加路由参数和回调
// 泛绑定
菜鸟实战,持续学习!
边栏推荐
- 选择排序和插入排序
- Redis安装部署(Windows/Linux)
- Installing Oracle database 19C RAC on Linux
- gocv拆分颜色通道
- What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
- Multi version concurrency control mvcc of MySQL
- HackTheBox-Gunship
- C language custom type enumeration, Union (clever use of enumeration, calculation of union size)
- Qunhui NAS configuring iSCSI storage
- C# 调用系统声音 嘀~
猜你喜欢

OpenShift 容器平台社区版 OKD 4.10.0部署

Minecraft群组服开服

WSL安装、美化、网络代理和远程开发

Minecraft空岛服开服

Solution and analysis of Hanoi Tower problem

Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away

commands out of sync. did you run multiple statements at once

How to realize asynchronous programming in a synchronous way?

Minecraft群組服開服

查看was发布的应用程序的端口
随机推荐
Image transformation, transpose
Minecraft群组服开服
zipkin 简单使用
Npoi export word font size correspondence
Linux binary installation Oracle database 19C
1、 QT's core class QObject
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
C#钉钉开发:取得所有员工通讯录和发送工作通知
Oracle related statistics
Openfeign facile à utiliser
OpenShift 容器平台社区版 OKD 4.10.0部署
Redis安装部署(Windows/Linux)
History of Web Technology
[flask] ORM one-to-one relationship
判断是否是数独
Essay: RGB image color separation (with code)
Solution of Xiaomi TV's inability to access computer shared files
C language implementation of mine sweeping game
kubeadm部署kubernetes v1.23.5集群
Benefits of ufcs of D

