-
Notifications
You must be signed in to change notification settings - Fork 26
/
defer.go
44 lines (36 loc) · 890 Bytes
/
defer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"fmt"
"os"
)
/*
Defer is used to ensure that a function call is performed later in a program’s execution,
usually for purposes of cleanup.
defer is often used where e.g. ensure and finally would be used in other languages.
*/
// Defer func to illustrate deffer in go
func Defer() {
// Suppose we wanted to create a file,
// write to it, and then close when we’re done.
// Here’s how we could do that with defer.
f := createFile("/tmp/defer.txt")
defer closeFile(f)
writeFile(f)
}
func createFile(p string) *os.File {
fmt.Println("Creating ...")
f, err := os.Create(p)
if err != nil {
panic(err)
}
return f
}
func writeFile(f *os.File) {
fmt.Println("Writing ...")
fmt.Fprintln(f, "data")
}
func closeFile(f *os.File) {
fmt.Println("Closing ...")
f.Close()
}
// Running the program confirms that the file is closed after being written.