当前位置:网站首页>【Go实战基础】gin 如何设置路由
【Go实战基础】gin 如何设置路由
2022-07-02 06:33:00 【菜鸟实战】
目录
一、简介
实战场景: 如何使用 gin 设置路由
二、知识点
- gin 框架启动
- 基础路由
- http get / post 请求
- 返回 字符串 string 数据
三、菜鸟实战
实战场景:使用 gin 设置路由
马上安排!
1、创建 go文件
/*
* @Author: 菜鸟实战
* @Description: gin 如何设置路由
*/
// 知识点:
// # gin 框架启动
// # 基础路由
// # http GET / POST 请求
// # 返回 字符串 String 数据
package main
// 导入包
import (
"fmt"
"github.com/gin-gonic/gin"
"runtime"
)
// 主函数
func main() {
// 使用内置函数打印
println("Hello", "菜鸟实战")
println("实战场景: ", "gin 如何设置路由")
// 初始化
r := gin.Default()
// 添加 get 路由和回调
r.GET("/get", func(c *gin.Context) {
// 返回的 code 和 字符串返回
c.String(200, "这是一个 get 方法\n")
})
// 添加 post 路由和回调
r.POST("/post", func(c *gin.Context) {
// 返回的 code 和 字符串返回
c.String(200, "这是一个 post 方法\n")
})
// 添加 delete 路由和回调
r.Handle("DELETE", "/delete", func(c *gin.Context) {
// 返回的 code 和 字符串返回
c.String(200, "这是一个 delete 方法\n")
})
// 添加 any 路由和回调
r.Any("/any", func(c *gin.Context) {
// 返回的 code 和 字符串返回
c.String(200, "这是一个 any 方法\n")
})
// 使用包函数打印
fmt.Printf("版本: %s \n", runtime.Version())
// 启动框架程序, 默认 8080 端口
r.Run()
}
// curl 验证方式
// curl -X GET "http://127.0.0.1:8080/get"
// curl -X POST "http://127.0.0.1:8080/post"
// curl -X DELETE "http://127.0.0.1:8080/delete"
// curl -X GET "http://127.0.0.1:8080/any"
// curl -X POST "http://127.0.0.1:8080/any"
// curl -X PUT "http://127.0.0.1:8080/any"
2、运行结果
Hello 菜鸟实战
实战场景: gin 如何设置路由
[GIN-debug] Listening and serving HTTP on :8080
//添加 get 路由和回调
//添加 post 路由和回调
//添加 delete 路由和回调
//添加 any 路由和回调
菜鸟实战,持续学习!
边栏推荐
- During MySQL installation, mysqld Exe reports that the application cannot start normally (0xc000007b)`
- Connect function and disconnect function of QT
- Qt QTimer类
- Minecraft安装资源包
- Benefits of ufcs of D
- First week of JS study
- Zipkin is easy to use
- NPOI 导出Word 字号对应
- Call Stack
- Analysis and solution of a classical Joseph problem
猜你喜欢
What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
Minecraft安装资源包
OpenFeign 简单使用
Data asset management function
群辉 NAS 配置 iSCSI 存储
Openshift build image
Solution and analysis of Hanoi Tower problem
OpenShift 容器平台社区版 OKD 4.10.0部署
Hengyuan cloud_ Can aiphacode replace programmers?
C language - Blue Bridge Cup - 7 segment code
随机推荐
Dip1000 implicitly tagged with fields
Honeypot attack and defense drill landing application scheme
Image transformation, transpose
Chrome debugging
C# 调用系统声音 嘀~
gocv opencv exit status 3221225785
Openfeign is easy to use
Detailed explanation of NIN network
NPOI 导出Word 字号对应
Win10 uses docker to pull the redis image and reports an error read only file system: unknown
[flask] ORM one-to-one relationship
一、Qt的核心类QObject
随笔:RGB图像颜色分离(附代码)
AMQ6126问题解决思路
OpenShift构建镜像
gocv opencv exit status 3221225785
Googlenet network explanation and model building
Essay: RGB image color separation (with code)
Kubedm deploys kubernetes v1.23.5 cluster
Openshift build image