Why is the subsequent code not executed after the gin group request is returned?

middle := r.Group("", func(context *gin.Context) {
        requestMethod := context.Request.Method
        token := ""
        fmt.Println("request method===========>", requestMethod)
        if requestMethod == "GET" {
            token = context.DefaultQuery("token", "")
        } else if requestMethod == "POST" {
            token = context.DefaultPostForm("token", "")
        }
        fmt.Println("token================>", token)
        if token == "" {
            context.JSON(http.StatusOK, gin.H{
                "code": statusCode.CODE_TOKEN_EMPTY,
                "msg":  statusCode.GetCodeMsg(statusCode.CODE_TOKEN_EMPTY),
            })
            return
        }
        //redisuid
        redisClient := common.CreateReadRedisClient()
        val, err := redisClient.Get(common.UserLoginPrex + token).Result()
        fmt.Println("user id =============>", val, err)
        if err != nil {
            context.JSON(http.StatusOK, gin.H{
                "code": statusCode.CODE_REDIS_COMMAND_ERR,
                "msg":  statusCode.GetCodeMsg(statusCode.CODE_REDIS_COMMAND_ERR),
            })
            return
        }
        if val == "" {
            context.JSON(http.StatusOK, gin.H{
                "code": statusCode.CODE_TOKEN_INVALID,
                "msg":  statusCode.GetCodeMsg(statusCode.CODE_TOKEN_INVALID),
            })
            return
        } else {
            fmt.Println("uid------------->", val)
            fmt.Println("token------------->", token)
            context.Set("uid", val)
            context.Set("token", token)
        }
    })
    //
    middle.GET("/user/myFans", user.MyFans)

defines a middle, and creates an interface, but if token is empty or token is illegal, I return json information in middle. My understanding is that the json error message is returned in middle, and the subsequent user.MyFans should not be executed, but the actual situation is that this is still executed, resulting in an output like this:

{"code":1001,"msg":""}{"code":0,"data":{"allFansNum":0,"directFans":0,"normalOperatorNum":0,"recommendFans":0,"superOperatorNum":0},"msg":"ok"}

how to deal with the change here, directly output {"code": 1001, "msg": "system error"}, the subsequent code is not executed?

Apr.02,2021

context.Abort ()


if you want to terminate the execution of context.Abort (), context.Next ()

Menu