当前位置:网站首页>【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 路由和回调
菜鸟实战,持续学习!
边栏推荐
- Openfeign facile à utiliser
- 整理秒杀系统的面试必备!!!
- Analysis and solution of a classical Joseph problem
- 汉诺塔问题的求解与分析
- D interface and domain problems
- [blackmail virus data recovery] suffix Hydra blackmail virus
- There is a problem with MySQL installation (the service already exists)
- Openshift build image
- Pclpy projection filter -- projection of point cloud to cylinder
- C # save web pages as pictures (using WebBrowser)
猜你喜欢

Move a string of numbers backward in sequence

Minecraft group service opening

Linux二进制安装Oracle Database 19c

Finishing the interview essentials of secsha system!!!

Sqli labs (post type injection)

Minecraft群组服开服

汉诺塔问题的求解与分析
![[blackmail virus data recovery] suffix Crylock blackmail virus](/img/b2/8e3a65dd250b9194cfc175138c740c.jpg)
[blackmail virus data recovery] suffix Crylock blackmail virus

Kubesphere virtualization KSV installation experience

OpenFeign 簡單使用
随机推荐
Nacos download, start and configure MySQL database
Sqli labs level 8 (Boolean blind note)
Linux安装Oracle Database 19c RAC
Multi version concurrency control mvcc of MySQL
Minecraft插件服开服
Use of libusb
NPOI 导出Word 字号对应
Dip1000 runaway
Sentinel reports failed to fetch metric connection timeout and connection rejection
Qt的右键菜单
QT qtimer class
Service de groupe minecraft
WSL installation, beautification, network agent and remote development
Luogu greedy part of the backpack line segment covers the queue to receive water
Pyspark de duplication dropduplicates, distinct; withColumn、lit、col; unionByName、groupBy
Kubesphere virtualization KSV installation experience
Avoid breaking changes caused by modifying constructor input parameters
将一串数字顺序后移
Hengyuan cloud_ Can aiphacode replace programmers?
Qt的拖动事件



