Context: https://go.dev/tour/flowcontrol/12
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"
func main() {
defer fmt.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.
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
deferbe 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.