bandian
V2EX  ›  问与答

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

  •  
  •   bandian · Mar 8, 2021 · 1042 views
    This topic created in 1933 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   ·   958 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 22:24 · PVG 06:24 · LAX 15:24 · JFK 18:24
    ♥ Do have faith in what you're doing.