V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
bandian
V2EX  ›  问与答

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

  •  
  •   bandian · Mar 8, 2021 · 948 views
    This topic created in 1888 days ago, the information mentioned may be changed or developed.

    比如下面的代码,在返回 {"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",
    	})
    }
    
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   972 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 19:36 · PVG 03:36 · LAX 12:36 · JFK 15:36
    ♥ Do have faith in what you're doing.