运维八一 运维八一
首页
运维杂记
编程浅尝
周积跬步
专栏
生活
关于
收藏
  • 分类
  • 标签
  • 归档
Source (opens new window)

运维八一

运维,运维!
首页
运维杂记
编程浅尝
周积跬步
专栏
生活
关于
收藏
  • 分类
  • 标签
  • 归档
Source (opens new window)
  • Go

    • 前言

    • Go基础知识

    • Go基本语法

    • 实战项目:简单web服务

    • 基本数据类型

    • 内置运算符

    • 分支和循环

    • 函数 function

    • 结构体 struct

    • 方法 method

    • 实战项目:跟踪函数调用链

    • 接口 interface

    • 并发 concurrency

    • 指针

    • 实战项目:实现轻量级线程池

    • 实战项目:实现TCP服务器

    • go常用包

    • Gin框架

      • gin入门
      • gin工作流程
      • gin中间件
      • gin路由
        • gin请求
        • 数据绑定和校验
        • 响应返回
        • 路由分发
        • Cookie和Session
        • gin项目结构
        • GORM入门
        • 一对多关联查询
      • go随记

    • Python

    • Shell

    • Java

    • Vue

    • 前端

    • 编程浅尝
    • Go
    • Gin框架
    lyndon
    2022-06-27
    目录

    gin路由

    # 1. 路由

    # 1.1 基本路由(无参路由)

    gin 框架中采用的路由库是基于 httprouter (opens new window) 做的。

    package main
    import (
    	"fmt"
    	"net/http"
    	"github.com/gin-gonic/gin"
    )
    func HelloWorldHandler(c *gin.Context) {
    	// gin.Context,封装了request和response
    	c.String(http.StatusOK, "hello World!")
    }
    func main() {
    	// 1.创建路由
    	r := gin.Default()
    	// 2.绑定路由规则,执行的函数
    	// 基本路由 /hello/
    	r.GET("/hello", HelloWorldHandler)
    	// 3.监听端口,默认在8080
    	fmt.Println("运行地址:http://127.0.0.1:8000")
    	r.Run(":8000")
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20

    # 1.2 api参数

    可以通过Context的Param方法来获取API参数。

    package main
    import (
    	"fmt"
    	"net/http"
    	"github.com/gin-gonic/gin"
    )
    func GetBookDetailHandler(c *gin.Context) {
    	bookId := c.Param("id")
    	// gin.Context,封装了request和response
    	c.String(http.StatusOK, fmt.Sprintf("成功获取书籍详情:%s", bookId))
    }
    func main() {
    	// 1.创建路由
    	r := gin.Default()
    	// 2.绑定路由规则,执行的函数
    	// 基本路由 /book/24/
    	r.GET("/book/:id", GetBookDetailHandler)
    	// 3.监听端口,默认在8080
    	fmt.Println("运行地址:http://127.0.0.1:8000/book/24/")
    	r.Run(":8000")
    }
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22

    # 1.3 url参数

    • URL参数可以通过DefaultQuery()或Query()方法获取
    • DefaultQuery()若参数不存在,返回默认值,Query()若不存在,返回空串
    • http://127.0.0.1:8080/user?name=zhangsan
    package main
    import (
    	"fmt"
    	"net/http"
    	"github.com/gin-gonic/gin"
    )
    func GetUserDetailHandler(c *gin.Context) {
    	//username := c.DefaultQuery("name", "xxx")
    	username := c.Query("name")
    	// gin.Context,封装了request和response
    	c.String(http.StatusOK, fmt.Sprintf("姓名:%s", username))
    }
    func main() {
    	// 1.创建路由
    	r := gin.Default()
    	// 2.绑定路由规则,执行的函数
    	// 基本路由 /user?name=root
    	r.GET("/user/", GetUserDetailHandler)
    	// 3.监听端口,默认在8080
    	fmt.Println("运行地址:http://127.0.0.1:8000/user?name=root")
    	r.Run(":8000")
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22

    # 1.4 routes group

    routes group是为了管理一些相同的URL

    package main
    
    import (
       "github.com/gin-gonic/gin"
       "fmt"
    )
    
    // gin的helloWorld
    func main() {
       // 1.创建路由
       // 默认使用了2个中间件Logger(), Recovery()
       r := gin.Default()
       // 路由组1 ,处理GET请求
       v1 := r.Group("/v1")
       // {} 是书写规范
       {
          v1.GET("/login", login)
          v1.GET("submit", submit)
       }
       v2 := r.Group("/v2")
       {
          v2.POST("/login", login)
          v2.POST("/submit", submit)
       }
       r.Run(":8000")
    }
    
    func login(c *gin.Context) {
       name := c.DefaultQuery("name", "jack")
       c.String(200, fmt.Sprintf("hello %s\n", name))
    }
    
    func submit(c *gin.Context) {
       name := c.DefaultQuery("name", "lily")
       c.String(200, fmt.Sprintf("hello %s\n", name))
    }
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37

    # 1.5 路由重定向

    HTTP重定向

    r.GET("/test", func(c *gin.Context) {
    	c.Redirect(http.StatusMovedPermanently, "http://www.sogo.com/")
    })
    
    
    1
    2
    3
    4

    路由重定向

    r.GET("/test", func(c *gin.Context) {
        // 指定重定向的URL
        c.Request.URL.Path = "/test2"
        r.HandleContext(c)
    })
    r.GET("/test2", func(c *gin.Context) {
        c.JSON(http.StatusOK, gin.H{"hello": "world"})
    })
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    上次更新: 2022/06/27, 00:59:36
    gin中间件
    gin请求

    ← gin中间件 gin请求→

    最近更新
    01
    ctr和crictl显示镜像不一致
    03-13
    02
    alpine镜像集成常用数据库客户端
    03-13
    03
    create-cluster
    02-26
    更多文章>
    Theme by Vdoing | Copyright © 2015-2024 op81.com
    苏ICP备18041258号-2
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式