mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2025-02-16 13:43:13 +03:00
added defer in loop sample
This commit is contained in:
parent
fcdf36fbbc
commit
749f730fd8
@ -15,9 +15,15 @@ func nilFuncDefer() {
|
||||
// Do not use defer inside a loop unless you are sure about what you are doing. It may not work as expected.
|
||||
// However, in some situations it will be handy for instance,delegating the recursivity of a func to a defer.
|
||||
func deferInsideLoop() {
|
||||
|
||||
nums := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
|
||||
for _, v := range nums {
|
||||
defer func(v int) {
|
||||
fmt.Println(v)
|
||||
}(v)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
nilFuncDefer()
|
||||
// nilFuncDefer()
|
||||
deferInsideLoop()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user