Skip to content

Commit 02d9e81

Browse files
committed
Added
1 parent 939f32b commit 02d9e81

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

golang-defer.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ This will print the first print statement last and the last print statement firs
88
defer fmt.Println("Last")
99
fmt.Println("First")
1010

11-
This is most useful for deferring closing of files.
11+
You can also defer anonymous functions.
12+
13+
defer func() {
14+
// Do stuff here
15+
}()
16+
17+
Deferring most useful for the closing of files.
1218

1319
aFile, err := os.Open("filename")
1420
if err !=nil {
@@ -20,3 +26,4 @@ This is most useful for deferring closing of files.
2026
In the above, you have ensure the file is closed by the time the function or method ends with the defer aFile.Close() statement.
2127

2228
If you exit with os.Exit() or log.Fatal, for example, the defer call will not be called, but the runtime will close any open files for you.
29+

0 commit comments

Comments
 (0)