Exit
, ExitAll
, and ExitHook
Exit
: Only exits the current executing logic method without exiting the subsequent request flow, can be used as a substitute forreturn
.ExitAll
: Forcibly interrupts the current execution flow, and neither the subsequent logic in the current executing method nor all subsequent logic methods will be executed, commonly used for permission control.ExitHook
: When multipleHOOK
methods are matched to a route, theHOOK
methods are executed in the order of route matching priority by default. When calling theExitHook
method in aHOOK
method, subsequentHOOK
methods will not be executed, similar toHOOK
method overriding.- These three exit functions are only effective in service functions and
HOOK
event callback functions and cannot control the execution flow of middleware.
Since the ExitAll
and ExitHook
methods are rarely used at the application layer, only the use of the Exit
method is introduced here.
tip
The Exit*
flow exit features are implemented using the panic...recover...
mechanism at the underlying level, with CPU execution costs of approximately tens of nanoseconds (ns
), improving usability with minimal runtime overhead.
Exit
Return Method
package main
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
func main() {
s := g.Server()
s.BindHandler("/", func(r *ghttp.Request) {
if r.Get("type").Int() == 1 {
r.Response.Writeln("john")
}
r.Response.Writeln("smith")
})
s.SetPort(8199)
s.Run()
}
After execution, we visit http://127.0.0.1:8199/?type=1, and see the page output:
smith
Let’s slightly adjust the above code:
package main
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
func main() {
s := g.Server()
s.BindHandler("/", func(r *ghttp.Request) {
if r.Get("type").Int() == 1 {
r.Response.Writeln("john")
r.Exit()
}
r.Response.Writeln("smith")
})
s.SetPort(8199)
s.Run()
}
After execution, we visit http://127.0.0.1:8199/?type=1 again, and see the page output:
In addition, the Response
object provides many Write*Exit
methods, which indicate that the content is output and the Exit
method is immediately called to exit the current service method.