Defer, panic and recover in Go
This is a study note for YouTube: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=12079s
Except conditional statement and looping, you can also use defer, panic and recover to manage control flow in Go.
Defer
Deferred functions are executed in LIFO (Last In First Out) order. For exampe:
The result is:
End! Middle! Start!
Defer function is very useful, one example is that you can use refer function to do some connection closing, resource clean up tasks. For example, in below code, you can put a close function right after you establish the connection before you even start to use the connection. This is because the close function is deferred, it will only execute after the calling function exits.
Another important thing to mention is that the deferred functions only take the variable value from the time is deferred, the change after the deferred function call will not impact the input value in the deferred function. For example:
The result is:
start
You can see that the value is still "start" even though variable "a" was changed to "end" later.
Panic
Panic is kinda like Exception in other programming language like JAVA. When panic function is executed, it will log the the position where causes the panic and then exit.
The result is:
(base)MyLaptop$ go run src/github.com/mingdos/firstapp/Main.go
start
panic: something bad happened
goroutine 1 [running]:
main.main()
/Users/caimingda/go/src/github.com/mingdos/firstapp/Main.go:10 +0x95
exit status 2
When works with deferred function, The execution order is like below:
Normal execution -> (defer function calling) -> normal execution -> (panic happens) -> defer function execution -> panic function execution
The result is:
(base)MyLaptop$ go run src/github.com/mingdos/firstapp/Main.go
start
this was deferred
panic: something bad happened
goroutine 1 [running]:
main.main()
/Users/caimingda/go/src/github.com/mingdos/firstapp/Main.go:11 +0x95
exit status 2
Recover
The result is:
start Error: something bad happened
The recover() function does more than this actually. It will recover the execution from where after the caller function who cause the panic inside and continue to execute the rest.
The result is:
start about to panic Error: something bad happened end
If the defer function don't know how to handle the panic, it actually can redo a panic when it detect a panic. In this way, the caller function will still have the panic.
Reference: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=12079s
Comments
Post a Comment