当前位置:网站首页>【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 路由和回调
菜鸟实战,持续学习!
边栏推荐
- C Baidu map, Gaode map, Google map (GPS) longitude and latitude conversion
- 图像变换,转置
- Nacos 下载启动、配置 MySQL 数据库
- Zipkin is easy to use
- win10使用docker拉取redis镜像报错read-only file system: unknown
- Sentinel reports failed to fetch metric connection timeout and connection rejection
- cmd窗口中中文呈现乱码解决方法
- Find the node with the smallest value range in the linked list and move it to the front of the linked list
- C# 百度地图,高德地图,Google地图(GPS) 经纬度转换
- gocv opencv exit status 3221225785
猜你喜欢

commands out of sync. did you run multiple statements at once
![[blackmail virus data recovery] suffix Crylock blackmail virus](/img/b2/8e3a65dd250b9194cfc175138c740c.jpg)
[blackmail virus data recovery] suffix Crylock blackmail virus

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

Sqli labs Level 2

Solution and analysis of Hanoi Tower problem

Service de groupe minecraft

Minecraft模组服开服

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

Don't spend money, spend an hour to build your own blog website

cmd窗口中中文呈现乱码解决方法
随机推荐
1、 QT's core class QObject
Openfeign is easy to use
Mirror protocol of synthetic asset track
C call system sound beep~
Finishing the interview essentials of secsha system!!!
2022/2/13 summary
Analysis and solution of a classical Joseph problem
Solution and analysis of Hanoi Tower problem
C nail development: obtain all employee address books and send work notices
Sentinel 简单使用
Openshift deployment application
Nacos 下载启动、配置 MySQL 数据库
统计字符串中各类字符的个数
Qt的connect函数和disconnect函数
查看was发布的应用程序的端口
Qt——如何在QWidget中设置阴影效果
Right click menu of QT
C# 将网页保存为图片(利用WebBrowser)
Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
Minecraft模组服开服



