what is it?
pushes a function call onto a list. the list of saved calls is executed after the surrounding function returns
example
func CopyFile(dstName, srcName string) (written int64, err error) {
src, err := os.Open(srcName)
if err != nil {
return
}
defer src.Close()
dst, err := os.Create(dstName)
if err != nil {
return
}
defer dst.Close()
return io.Copy(dst, src)
}allows us to think about closing each file right after opening it
rules
-
a deferred function’s arguments are evaluated when the defer statement is evaluated
func a() { i := 0 defer fmt.Println(i) i++ return } // the deferred call will print 0 after the function returns -
deferred function calls are executed in last in first out order
func a() { i := 0 defer fmt.Println(i) i++ return } // prints 3210 -
deferred functions may read and assign to the returning function’s named return values
uses of defer
// releasing a mutex
mu.Lock()
defer mu.Unlock()
// printing a footer
printHeader()
defer printFooter()