You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Defer
A defer statement defers the execution of a function until the surrounding function returns.
The deferred call's arguments are evaluated immediately, but the function call is not executed until the surrounding function returns.
A brief explanation of why defering is necessary may be helpful. Because the example given seems contrived:
package main
import"fmt"funcmain() {
deferfmt.Println("world")
fmt.Println("hello")
}
A tyro may ask: why not the following instead?
fmt.Println("hello")
fmt.Println("world")
i.e. in what scenario would defer be necessary? It seems that in every case I can think of - you could achieve the same outcome by ordering the execution differently.
my 2 cents.
The text was updated successfully, but these errors were encountered:
benkoshy
changed the title
tour: defer - a short description on a genuine use for defer case may be helpful
tour: defer - a short description on a genuine use for defer may be helpful
Dec 16, 2024
Context: https://go.dev/tour/flowcontrol/12
A brief explanation of why defering is necessary may be helpful. Because the example given seems contrived:
A tyro may ask: why not the following instead?
i.e. in what scenario would
defer
be necessary? It seems that in every case I can think of - you could achieve the same outcome by ordering the execution differently.my 2 cents.
The text was updated successfully, but these errors were encountered: