V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
bandian
V2EX  ›  问与答

golang gin 如何在在返回数据之后就断开请求

  •  
  •   bandian · 2021-03-08 15:17:30 +08:00 · 526 次点击
    这是一个创建于 1145 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如下面的代码,在返回 {"hello":"world"} 之后如何停止本次请求,即使是在这个返回后面加上 c.Abort() 也是无效的,因为这个 Abort 断开的应该是 handler 的链路,对于调用 Abort 的 handler 中后续的逻辑是没有影响的。

    func main() {
    	router := gin.Default()
    	router.GET("/hello", Hello)
    	router.Run(":9999")
    }
    
    func Hello(ctx *gin.Context) {
    	ctx.JSON(200, gin.H{
    		"hello":"world",
    	})
    
    	ctx.JSON(500, gin.H{
    		"no":"no",
    	})
    }
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   969 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 20:16 · PVG 04:16 · LAX 13:16 · JFK 16:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.