好好爱自己!

gin使用中间件

原文:https://www.cnblogs.com/jachin/p/9966839.html

golang gin 中间件,返回结果

 

--------------

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
38
39
40
package main
  
import (
    "net/http"
    "github.com/gin-gonic/gin"
)
  
func response() gin.HandlerFunc {
    return func(c *gin.Context) {
        c.Next()
        if c.Writer.Written() {
            return
        }
  
        params := c.Keys
        if len(params) == 0 {
            return
        }
        c.JSON(http.StatusOK, params)
    }
}
  
func main() {
    r := gin.Default()
    r.Use(response())
  
    r.GET("/ping", func(c *gin.Context) {
        c.String(http.StatusOK, "PONG")
    })
  
    r.GET("/status", func(c *gin.Context) {
        c.Status(http.StatusOK)
    })
  
    r.GET("/hello", func(c *gin.Context) {
        c.Set("message", "hello, world")
    })
  
    r.Run()
}

  

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
➜  src  go run main.go
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
 
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)
 
[GIN-debug] GET    /ping                     --> main.main.func1 (4 handlers)
[GIN-debug] GET    /status                   --> main.main.func2 (4 handlers)
[GIN-debug] GET    /hello                    --> main.main.func3 (4 handlers)
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080
[GIN] 2018/11/15 - 23:23:23 | 200 |     128.732µs |       127.0.0.1 | GET      /ping
[GIN] 2018/11/15 - 23:23:26 | 200 |       1.764µs |       127.0.0.1 | GET      /status
[GIN] 2018/11/15 - 23:23:31 | 200 |      135.48µs |       127.0.0.1 | GET      /hello

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
➜  ~  http http://127.0.0.1:8080/ping
HTTP/1.1 200 OK
Content-Length: 4
Content-Type: text/plain; charset=utf-8
Date: Thu, 15 Nov 2018 15:23:23 GMT
 
PONG
 
➜  ~  http http://127.0.0.1:8080/status
HTTP/1.1 200 OK
Content-Length: 0
Date: Thu, 15 Nov 2018 15:23:26 GMT
 
 
 
➜  ~  http http://127.0.0.1:8080/hello
HTTP/1.1 200 OK
Content-Length: 26
Content-Type: application/json; charset=utf-8
Date: Thu, 15 Nov 2018 15:23:31 GMT
 
{
    "message""hello, world"
}

  

posted @   立志做一个好的程序员  阅读(114)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2019-10-10 Multiple inheritance in Go
2019-10-10 Inheritance and subclassing in Go - or its near likeness
2019-10-10 golang Methods on structs
2018-10-10 【转】6 Reasons Why JavaScript’s Async/Await Blows Promises Away (Tutorial)
2017-10-10 【转】shell中的内建命令, 函数和外部命令
2017-10-10 clear out all variables without closing terminal
2017-10-10 linux中shell命令test用法和举例

不断学习创作,与自己快乐相处

点击右上角即可分享
微信分享提示