当前位置:网站首页>【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 路由和回调
菜鸟实战,持续学习!
边栏推荐
猜你喜欢
[blackmail virus data recovery] suffix Crylock blackmail virus
Shengshihaotong and Guoao (Shenzhen) new energy Co., Ltd. build the charging pile industry chain
Synchronize files using unison
There is a problem with MySQL installation (the service already exists)
Service de groupe minecraft
Win10 uses docker to pull the redis image and reports an error read only file system: unknown
Minecraft module service opening
Mysql安装时mysqld.exe报`应用程序无法正常启动(0xc000007b)`
Nacos download, start and configure MySQL database
2022/2/13 summary
随机推荐
gocv边界填充
zipkin 简单使用
C Baidu map, Gaode map, Google map (GPS) longitude and latitude conversion
Gocv split color channel
Detailed explanation of NIN network
Solution and analysis of Hanoi Tower problem
Minecraft air Island service
Leetcode sword finger offer brush questions - day 22
Webflux responsive programming
C language - Blue Bridge Cup - 7 segment code
Dip1000 runaway
gocv拆分颜色通道
Sqli labs Level 2
Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
gocv opencv exit status 3221225785
MYSQL安装出现问题(The service already exists)
AMQ6126问题解决思路
Googlenet network explanation and model building
Oracle related statistics
C#钉钉开发:取得所有员工通讯录和发送工作通知