当前位置:网站首页>【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 路由和回调
菜鸟实战,持续学习!
边栏推荐
- Nacos download, start and configure MySQL database
- OpenShift 容器平台社区版 OKD 4.10.0部署
- Redis安装部署(Windows/Linux)
- Qt的connect函数和disconnect函数
- How to realize asynchronous programming in a synchronous way?
- Viewing JS array through V8
- Qt QTimer类
- gocv opencv exit status 3221225785
- C Gaode map obtains the address according to longitude and latitude
- Use of libusb
猜你喜欢

Minecraft module service opening

Data asset management function

QT -- how to set shadow effect in QWidget
![[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

Chrome debugging

Finishing the interview essentials of secsha system!!!

Classes and objects (instantiation of classes and classes, this, static keyword, encapsulation)

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

Minecraft group service opening
随机推荐
Data asset management function
Qt的右键菜单
2022/2/13 summary
OpenFeign 简单使用
C# 调用系统声音 嘀~
Openshift deployment application
Loadbalancer dynamically refreshes Nacos server
Tcp/ip - transport layer
汉诺塔问题的求解与分析
C# 将网页保存为图片(利用WebBrowser)
Pclpy projection filter -- projection of point cloud to cylinder
C language - Blue Bridge Cup - 7 segment code
Installing Oracle database 19C for Linux
Minecraft group service opening
QT -- how to set shadow effect in QWidget
将一串数字顺序后移
Find the node with the smallest value range in the linked list and move it to the front of the linked list
Finishing the interview essentials of secsha system!!!
NPOI 导出Word 字号对应
Avoid breaking changes caused by modifying constructor input parameters



