mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-22 12:46:03 +03:00
added defer nil func example
This commit is contained in:
parent
e4dc35ec74
commit
9b491561e5
17
defer/main.go
Normal file
17
defer/main.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// Sample code is taken from online tutorial https://blog.learngoprogramming.com/gotchas-of-defer-in-go-1-8d070894cb01
|
||||||
|
// it demostrate all the common GOTCHA when using defer.
|
||||||
|
|
||||||
|
// If a deferred func evaluates to nil, execution panic when surrounding func ends not when defer is called
|
||||||
|
func nilFuncDefer() {
|
||||||
|
var run func() = nil
|
||||||
|
defer run()
|
||||||
|
fmt.Println("runs")
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
nilFuncDefer()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user